繼續學習之Gizmos in the Scene View的自己寫的一個無聊的效果

說明:在 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 = 60;
    [SerializeField]
    private float _gravity = -30;
    [SerializeField]
    private AudioClip _bgm;
    [SerializeField]
    private Sprite _background;
    [SerializeField]
    private int _totalColumns = 25;
    [SerializeField]
    private int _totalRows = 10;

    public const float GridSize = 1f;
    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(0, 0, 0), new Vector3(0, rows *
        GridSize, 0));
        Gizmos.DrawLine(new Vector3(0, 0, 0), new Vector3(cols *
        GridSize, 0, 0));
        Gizmos.DrawLine(new Vector3(cols * GridSize, 0, 0), new
        Vector3(cols * GridSize, rows * GridSize, 0));
        Gizmos.DrawLine(new Vector3(0, rows * GridSize, 0), new
        Vector3(cols * GridSize, rows * GridSize, 0));
    }

    //繪製內部的線條
    private void GridGizmo(int cols, int rows)
    {
        for (int i = 1; i < cols; i++)
        {
            Gizmos.DrawLine(new Vector3(i * GridSize, 0, 0), new Vector3(i
            * GridSize, rows * GridSize, 0));
        }
        for (int j = 1; j < rows; j++)
        {
            Gizmos.DrawLine(new Vector3(0, j * GridSize, 0), new
            Vector3(cols * GridSize, j * GridSize, 0));
        }
    }

    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), 0.0f);
        return gridPoint;
    }

    public Vector3 GridToWorldCoordinates(int col, int row)
    {
        Vector3 worldPoint = new Vector3(
        transform.position.x + (col * GridSize + GridSize / 2.0f),
        transform.position.y + (row * GridSize + GridSize / 2.0f),
        0.0f);
        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 >= 0 && col < _totalColumns && row >= 0 && row < _totalRows);
    }

}

腳本 :MoveMgr.cs

using UnityEngine;
using System.Collections;

public class MoveMgr : MonoBehaviour {
    [SerializeField]
    public int m_count = 10;
    public GameObject prefab;
    // Use this for initialization
    void Start () {
        for (int i = 0; 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(0, 1, 0),   
                                    new Vector3(0, -1, 0),
                                    new Vector3(1, 0, 0),
                                    new Vector3(-1, 0, 0)
                                 };
    //當前啓動移動的方向
    private Vector3 m_currentDirection = Vector3.zero;
    // Use this for initialization
    public override void Start () {
        base.Start();
        this.m_pointX = Random.Range(0, this.m_totalColumns+1);
        this.m_pointY = Random.Range(0, this.m_totalRows + 1);
        this.transform.localPosition = new Vector3(this.m_pointX,this.m_pointY,0);
        m_currentDirection = m_direction[Random.Range(0, 4)]; 
    }

    // 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(0, 4)];
        }
        this.transform.localPosition += m_currentDirection;
    }

    //三秒改變一個方向
    public override void ThreeSecondContorlMove()
    {
        if (!m_isOverBoder)
        {
            m_currentDirection = m_direction[Random.Range(0, 4)];
        }
    }

}

腳本 sixsixsix.cs(這是一個基類 簡單的封裝 可能我封裝的很垃圾,學習者慢慢封裝吧)

using UnityEngine;
using System.Collections;

public class sixsixsix : MonoBehaviour {

    // Use this for initialization
    private test m_t;

    protected int m_pointX = 0;
    protected int m_pointY = 0;

    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(1);
            this.MoveOneSecond();

        }
    }

    IEnumerator ControlMove()
    {
        while (true)
        {
            yield return new WaitForSeconds(3);
            this.ThreeSecondContorlMove();
        }
    }

    public virtual void MoveOneSecond() { }

    public virtual void ThreeSecondContorlMove() { }
}

學習了Gizmos這個東西 至少我工作中沒用到 我看書中寫的主要是用在關卡編輯器上 , 寫上面這個東西 我也是隨便的一個想法 ,這裏做一下筆記

以上代碼都有註釋 麼有的 我的另一邊學習Gizmos中有提到 Gizmosde 的一些函數 ,以上基礎都是來之《Extending Unity With Editor》,如有錯誤請告知

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章