該源代碼轉載自Unity遊戲案例中的TANKS代碼中
------------來自第二次使用Unity3D製作遊戲的遊戲製作新人小白
一、代碼自我解析
二、油管學習地址
三、Unity3D源代碼
一、源代碼自我解析
using System;
using UnityEngine;
[Serializable]
public class TankManager
{
public Color m_PlayerColor; // 設置玩家的顏色
public Transform m_SpawnPoint; // 設置坦克的起始位置和朝向
[HideInInspector] public int m_PlayerNumber; // 玩家名
[HideInInspector] public string m_ColoredPlayerText; // 比賽裏玩家的名字對應的顏色
[HideInInspector] public GameObject m_Instance; // 加載坦克實例
[HideInInspector] public int m_Wins; // 判斷誰贏了(玩家號)
private TankMovement m_Movement; // 設置坦克移動
private TankShooting m_Shooting; // 設置坦克炮彈發射
private GameObject m_CanvasGameObject; // 設置畫布計分板以及判斷誰贏了
public void Setup()
{
// 獲取你加載實例的各項數據
m_Movement = m_Instance.GetComponent<TankMovement>();
m_Shooting = m_Instance.GetComponent<TankShooting>();
m_CanvasGameObject = m_Instance.GetComponentInChildren<Canvas>().gameObject;
// 將玩家各自對應起來(綁定各自玩家的腳本)前面寫的腳本都是通用的,每個玩家都能夠copy一份
m_Movement.m_PlayerNumber = m_PlayerNumber;
m_Shooting.m_PlayerNumber = m_PlayerNumber;
// 設置玩家的顏色
m_ColoredPlayerText = "<color=#" + ColorUtility.ToHtmlStringRGB(m_PlayerColor) + ">PLAYER " + m_PlayerNumber + "</color>";
// 遍歷渲染器中的坦克皮膚頂點(並對每個頂點都賦予新的顏色)(換皮膚)
MeshRenderer[] renderers = m_Instance.GetComponentsInChildren<MeshRenderer>();
for (int i = 0; i < renderers.Length; i++)
{
renderers[i].material.color = m_PlayerColor;
}
}
public void DisableControl()
{
// 禁用玩家控制
m_Movement.enabled = false;
m_Shooting.enabled = false;
m_CanvasGameObject.SetActive(false);
}
public void EnableControl()
{
// 啓用玩家控制
m_Movement.enabled = true;
m_Shooting.enabled = true;
m_CanvasGameObject.SetActive(true);
}
public void Reset()
{
// 重置玩家,遊戲本身就是回合制的,好像是誰先贏五誰就勝利
m_Instance.transform.position = m_SpawnPoint.position;
m_Instance.transform.rotation = m_SpawnPoint.rotation;
m_Instance.SetActive(false);
m_Instance.SetActive(true);
}
}
// 以上只是自己對於該代碼的理解,如有誤還望指出讓我及時改正。
二、油管學習Unity地址
https://www.youtube.com/watch?v=paLLfWd2k5A
三、Unity3D中該案例源代碼:
using System;
using UnityEngine;
namespace Complete
{
[Serializable]
public class TankManager
{
// This class is to manage various settings on a tank.
// It works with the GameManager class to control how the tanks behave
// and whether or not players have control of their tank in the
// different phases of the game.
public Color m_PlayerColor; // This is the color this tank will be tinted.
public Transform m_SpawnPoint; // The position and direction the tank will have when it spawns.
[HideInInspector] public int m_PlayerNumber; // This specifies which player this the manager for.
[HideInInspector] public string m_ColoredPlayerText; // A string that represents the player with their number colored to match their tank.
[HideInInspector] public GameObject m_Instance; // A reference to the instance of the tank when it is created.
[HideInInspector] public int m_Wins; // The number of wins this player has so far.
private TankMovement m_Movement; // Reference to tank's movement script, used to disable and enable control.
private TankShooting m_Shooting; // Reference to tank's shooting script, used to disable and enable control.
private GameObject m_CanvasGameObject; // Used to disable the world space UI during the Starting and Ending phases of each round.
public void Setup ()
{
// Get references to the components.
m_Movement = m_Instance.GetComponent<TankMovement> ();
m_Shooting = m_Instance.GetComponent<TankShooting> ();
m_CanvasGameObject = m_Instance.GetComponentInChildren<Canvas> ().gameObject;
// Set the player numbers to be consistent across the scripts.
m_Movement.m_PlayerNumber = m_PlayerNumber;
m_Shooting.m_PlayerNumber = m_PlayerNumber;
// Create a string using the correct color that says 'PLAYER 1' etc based on the tank's color and the player's number.
m_ColoredPlayerText = "<color=#" + ColorUtility.ToHtmlStringRGB(m_PlayerColor) + ">PLAYER " + m_PlayerNumber + "</color>";
// Get all of the renderers of the tank.
MeshRenderer[] renderers = m_Instance.GetComponentsInChildren<MeshRenderer> ();
// Go through all the renderers...
for (int i = 0; i < renderers.Length; i++)
{
// ... set their material color to the color specific to this tank.
renderers[i].material.color = m_PlayerColor;
}
}
// Used during the phases of the game where the player shouldn't be able to control their tank.
public void DisableControl ()
{
m_Movement.enabled = false;
m_Shooting.enabled = false;
m_CanvasGameObject.SetActive (false);
}
// Used during the phases of the game where the player should be able to control their tank.
public void EnableControl ()
{
m_Movement.enabled = true;
m_Shooting.enabled = true;
m_CanvasGameObject.SetActive (true);
}
// Used at the start of each round to put the tank into it's default state.
public void Reset ()
{
m_Instance.transform.position = m_SpawnPoint.position;
m_Instance.transform.rotation = m_SpawnPoint.rotation;
m_Instance.SetActive (false);
m_Instance.SetActive (true);
}
}
}