說明:在 Scene視圖 下 利用Gizmos線框 實作了一個 很多個小怪在地圖裡面 随機 走動 (目前碰到邊緣我是讓他走回來)
效果圖

untiy的Hierarchy
1 網格 (建立一個空物體 挂test.cs腳本(代碼在下面))
2 小怪管理類 (空物體 挂 MoveMgr.cs)
3 小怪 (空物體 挂的是 move)
腳本:test.cs
using UnityEngine;
using System.Collections;
public class test : MonoBehaviour {
[SerializeField]
public int _totalTime = ;
[SerializeField]
private float _gravity = -;
[SerializeField]
private AudioClip _bgm;
[SerializeField]
private Sprite _background;
[SerializeField]
private int _totalColumns = ;
[SerializeField]
private int _totalRows = ;
public const float GridSize = f;
private readonly Color _normalColor = Color.grey;
private readonly Color _selectedColor = Color.yellow;
public int TotalTime
{
get { return _totalTime; }
set { _totalTime = value; }
}
public float Gravity
{
get { return _gravity; }
set { _gravity = value; }
}
public AudioClip Bgm
{
get { return _bgm; }
set { _bgm = value; }
}
public Sprite Background
{
get { return _background; }
set { _background = value; }
}
public int TotalColumns
{
get { return _totalColumns; }
set { _totalColumns = value; }
}
public int TotalRows
{
get { return _totalRows; }
set { _totalRows = value; }
}
public test Instance { get { return this; } }
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
//繪制外框
private void GridFrameGizmo(int cols, int rows)
{
Gizmos.DrawLine(new Vector3(, , ), new Vector3(, rows *
GridSize, ));
Gizmos.DrawLine(new Vector3(, , ), new Vector3(cols *
GridSize, , ));
Gizmos.DrawLine(new Vector3(cols * GridSize, , ), new
Vector3(cols * GridSize, rows * GridSize, ));
Gizmos.DrawLine(new Vector3(, rows * GridSize, ), new
Vector3(cols * GridSize, rows * GridSize, ));
}
//繪制内部的線條
private void GridGizmo(int cols, int rows)
{
for (int i = ; i < cols; i++)
{
Gizmos.DrawLine(new Vector3(i * GridSize, , ), new Vector3(i
* GridSize, rows * GridSize, ));
}
for (int j = ; j < rows; j++)
{
Gizmos.DrawLine(new Vector3(, j * GridSize, ), new
Vector3(cols * GridSize, j * GridSize, ));
}
}
private void OnDrawGizmos()
{
Color oldColor = Gizmos.color;
Matrix4x4 oldMatrix = Gizmos.matrix;
Gizmos.matrix = transform.localToWorldMatrix;
Gizmos.color = _normalColor;
GridGizmo(_totalColumns, _totalRows);
GridFrameGizmo(_totalColumns, _totalRows);
Gizmos.color = oldColor;
Gizmos.matrix = oldMatrix;
}
private void OnDrawGizmosSelected()
{
Color oldColor = Gizmos.color;
Matrix4x4 oldMatrix = Gizmos.matrix;
Gizmos.matrix = transform.localToWorldMatrix;
Gizmos.color = _selectedColor;
GridFrameGizmo(_totalColumns, _totalRows);
Gizmos.color = oldColor;
Gizmos.matrix = oldMatrix;
}
public Vector3 WorldToGridCoordinates(Vector3 point)
{
Vector3 gridPoint = new Vector3(
(int)((point.x - transform.position.x) / GridSize),
(int)((point.y - transform.position.y) / GridSize), f);
return gridPoint;
}
public Vector3 GridToWorldCoordinates(int col, int row)
{
Vector3 worldPoint = new Vector3(
transform.position.x + (col * GridSize + GridSize / f),
transform.position.y + (row * GridSize + GridSize / f),
f);
return worldPoint;
}
public bool IsInsideGridBounds(Vector3 point)
{
float minX = transform.position.x;
float maxX = minX + _totalColumns * GridSize;
float minY = transform.position.y;
float maxY = minY + _totalRows * GridSize;
return (point.x >= minX && point.x <= maxX && point.y >= minY &&
point.y <= maxY);
}
public bool IsInsideGridBounds(int col, int row)
{
return (col >= && col < _totalColumns && row >= && row < _totalRows);
}
}
腳本 :MoveMgr.cs
using UnityEngine;
using System.Collections;
public class MoveMgr : MonoBehaviour {
[SerializeField]
public int m_count = ;
public GameObject prefab;
// Use this for initialization
void Start () {
for (int i = ; i < m_count; i++)
{
GameObject.Instantiate(prefab);
}
}
// Update is called once per frame
void Update () {
}
}
腳本:move.cs
using UnityEngine;
using System.Collections;
public class move : sixsixsix {
//方向數組 上下左右
private Vector3[] m_direction = { new Vector3(, , ),
new Vector3(, -, ),
new Vector3(, , ),
new Vector3(-, , )
};
//目前啟動移動的方向
private Vector3 m_currentDirection = Vector3.zero;
// Use this for initialization
public override void Start () {
base.Start();
this.m_pointX = Random.Range(, this.m_totalColumns+);
this.m_pointY = Random.Range(, this.m_totalRows + );
this.transform.localPosition = new Vector3(this.m_pointX,this.m_pointY,);
m_currentDirection = m_direction[Random.Range(, )];
}
// Update is called once per frame
public override void Update () {
base.Update();
}
//每隔一秒運作
public override void MoveOneSecond()
{
if (m_isOverBoder)
{
//如果越界 讓它傳回
m_currentDirection = -m_currentDirection;
this.transform.localPosition += m_currentDirection;
//傳回後重新選擇方向
m_currentDirection = m_direction[Random.Range(, )];
}
this.transform.localPosition += m_currentDirection;
}
//三秒改變一個方向
public override void ThreeSecondContorlMove()
{
if (!m_isOverBoder)
{
m_currentDirection = m_direction[Random.Range(, )];
}
}
}
腳本 sixsixsix.cs(這是一個基類 簡單的封裝 可能我封裝的很垃圾,學習者慢慢封裝吧)
using UnityEngine;
using System.Collections;
public class sixsixsix : MonoBehaviour {
// Use this for initialization
private test m_t;
protected int m_pointX = ;
protected int m_pointY = ;
protected int m_totalColumns;
protected int m_totalRows;
[SerializeField]
protected bool m_isOverBoder;
public virtual void Awake()
{
m_t = GameObject.Find("Grid").GetComponent<test>();
m_totalColumns = m_t.TotalColumns;
m_totalRows = m_t.TotalRows;
}
public virtual void Start()
{
StartCoroutine("Move");
StartCoroutine("ControlMove");
}
// Update is called once per frame
public virtual void Update()
{
Vector3 gridCoord = m_t.WorldToGridCoordinates
(transform.position);
transform.position = m_t.GridToWorldCoordinates((int)gridCoord.x, (int)gridCoord.y);
}
//畫每一個物體的樣子
private void OnDrawGizmos()
{
Color oldColor = Gizmos.color;
Gizmos.color = (m_t.IsInsideGridBounds(transform.
position)) ? Color.green : Color.red;
Gizmos.DrawCube(transform.position, Vector3.one * test.GridSize);
Gizmos.color = oldColor;
//記錄是否出界
m_isOverBoder = !m_t.IsInsideGridBounds(transform.position);
}
IEnumerator Move()
{
while (true)
{
yield return new WaitForSeconds();
this.MoveOneSecond();
}
}
IEnumerator ControlMove()
{
while (true)
{
yield return new WaitForSeconds();
this.ThreeSecondContorlMove();
}
}
public virtual void MoveOneSecond() { }
public virtual void ThreeSecondContorlMove() { }
}
學習了Gizmos這個東西 至少我工作中沒用到 我看書中寫的主要是用在關卡編輯器上 , 寫上面這個東西 我也是随便的一個想法 ,這裡做一下筆記
以上代碼都有注釋 麼有的 我的另一邊學習Gizmos中有提到 Gizmosde 的一些函數 ,以上基礎都是來之《Extending Unity With Editor》,如有錯誤請告知