游戏对象和资源的联系:
游戏对象是在游戏中存在的一个个例,可以是可视的物体,比如你操纵的人物,你所对话的人物,你人物上佩戴的项链等等都是游戏对象;
资源则是对于游戏对象的辅助描述,相当于修饰。比如石壁是一个游戏对象,那么石壁上的纹理就是一种资源。
游戏案例:
资源的目录组织结构包括了:
Prefabs预设,resources动态加载的资源文件,Scenes场景文件,Scenes场景文件,Scripts脚本代码文件,Sounds音效文件,Textures所有的贴图
游戏对象:
则是游戏中的实体,同时他们之间一般使用了继承。
debug代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScriptTest : MonoBehaviour {
void Awake()
{
Debug.Log("Awake!");
}
void Start()
{
Debug.Log("Start!");
}
void Update()
{
Debug.Log("Update!");
}
void FixedUpdate()
{
Debug.Log("FixedUpdate!");
}
void LateUpdate()
{
Debug.Log("LateUpdate!");
}
void Reset()
{
Debug.Log("Reset!");
}
void OnGUI()
{
Debug.Log("onGUI!");
}
void OnDisable()
{
Debug.Log("onDisable!");
}
void OnDestroy()
{
Debug.Log("onDestroy!");
}
}
查找脚本手册,了解GameObject ,Transform,Component对象
GameObject: 游戏中的每个对象都是一个游戏对象。然而,游戏对象本身不做任何事情。它们需要特殊属性才能成为一个角色、一种环境或者一种特殊效果。
Transform:变换是每个游戏对象的关键组件。它们决定游戏对象的位置、旋转方式及缩放。
Reset:在游戏中,组件就是对象和行为的螺栓与螺母,它们是每个游戏对象 的功能零件。
table的属性
table 对象(实体)的属性:activeSelf、isStatic、Layer、Tag、Transform
table 的 Transform 的属性:Position、Rotation、Scale
table 的部件:Mesh Filter、Box Collider、Mesh Renderer
预设的原因
-
预设在被修改时可以保持所有副本同步,且能够提高使用资源的效率。
-
修改预设时,预设的副本也会发生变化。修改克隆对象时,其副本是独立于被克隆的对象,不会发生变化。
-
public GameObject table; void Start () { GameObject instance = (GameObject)Instantiate(table, transform.position, transform.rotation); } void Update () {}
TicTacToe.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TicTacToe : MonoBehaviour
{
int turn;
int count;
int[,] map = new int[3, 3];
// Use this for initialization
void Start ()
{
Init();
}
//初始化界面
void Init()
{
turn = 1;
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
map[i, j] = 0;
}
}
count = 0;
}
//验证游戏是否结束
int Check()
{
for(int i = 0; i < 3; ++i)
{
if (map[i, 0] != 0 && map[i,0]==map[i, 1] && map[i, 1] == map[i, 2])
{
return map[i, 0];
}
}
for (int i = 0; i < 3; ++i)
{
if (map[0, i] != 0 && map[0, i] == map[1, i] && map[1, i] == map[2, i])
{
return map[0, i];
}
}
if(map[1,1]!=0&&
map[0,0]==map[1,1]&&
map[1,1]==map[2,2]||
map[0,2]==map[1,1]&&
map[1,1]==map[2,0]
)
return map[1, 1];
if (count == 9) return 2;
return 0;
}
//构建UI界面
void OnGUI()
{
if(GUI.Button(new Rect(420, 300, 100, 50),"Restart"))
{
Init();
}
int result = Check();
GUIStyle end = new GUIStyle
{
fontSize = 20
};
end.normal.textColor = Color.red;
end.fontStyle = FontStyle.BoldAndItalic;
GUIStyle hit = new GUIStyle
{
};
if(result == 1)
GUI.Label(new Rect(440, 200, 100, 50), "O WIN", style: end);
else if (result == -1)
GUI.Label(new Rect(440, 200, 100, 50), "X WIN", style: end);
else if(result == 2)
GUI.Label(new Rect(440, 200, 100, 50), "DUAL", style: end);
for (int i = 0; i < 3; ++i)
{
for(int j = 0; j < 3; ++j)
{
if (map[i, j] == 1)
{
GUI.Button(new Rect(400 + i * 50, j * 50, 50, 50), "O");
}
if (map[i, j] == -1)
{
GUI.Button(new Rect(400 + i * 50, j * 50, 50, 50), "X");
}
if(GUI.Button(new Rect(400 + i * 50, j * 50, 50, 50), ""))
{
if (result == 0)
{
map[i, j] = turn;
++count;
turn = -turn;
}
}
}
}
}
// Update is called once per frame
void Update ()
{
}
}