继续学习之Gizmos in the Scene View的自己写的一个无聊的效果
2017-03-01 22:56
405 查看
说明:在 Scene视图 下 利用Gizmos线框 实现了一个 很多个小怪在地图里面 随机 走动 (目前碰到边缘我是让他走回来)
效果图
untiy的Hierarchy
1 网格 (新建一个空物体 挂test.cs脚本(代码在下面))
2 小怪管理类 (空物体 挂 MoveMgr.cs)
3 小怪 (空物体 挂的是 move)
脚本:test.cs
脚本 :MoveMgr.cs
脚本:move.cs
脚本 sixsixsix.cs(这是一个基类 简单的封装 可能我封装的很垃圾,学习者慢慢封装吧)
学习了Gizmos这个东西 至少我工作中没用到 我看书中写的主要是用在关卡编辑器上 , 写上面这个东西 我也是随便的一个想法 ,这里做一下笔记
以上代码都有注释 么有的 我的另一边学习Gizmos中有提到 Gizmosde 的一些函数 ,以上基础都是来之《Extending Unity With Editor》,如有错误请告知
效果图
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 (tr af10 ue) { yield return new WaitForSeconds(3); this.ThreeSecondContorlMove(); } } public virtual void MoveOneSecond() { } public virtual void ThreeSecondContorlMove() { } }
学习了Gizmos这个东西 至少我工作中没用到 我看书中写的主要是用在关卡编辑器上 , 写上面这个东西 我也是随便的一个想法 ,这里做一下笔记
以上代码都有注释 么有的 我的另一边学习Gizmos中有提到 Gizmosde 的一些函数 ,以上基础都是来之《Extending Unity With Editor》,如有错误请告知
相关文章推荐
- 周末闲着无聊分享一个自己写的带呼吸效果的android水波纹自定义view
- In an iOS 5 Storyboard, how do you push a new scene to the original view controller from a Popover?
- 深度学习论文笔记--Recover Canonical-View Faces in the Wild with Deep Neural Network
- android 给LinearLayout中添加一定数量的控件,并让着一定数量的控件从右到左移动,每隔若干秒停顿一下,最后一个view链接第一个view,然后继续移动循环往复,形成一个死循环简单动画效果
- 大光头的shader学习之路-自己写的一个消融效果
- 关于OpenSessionInView的学习和使用
- 一个JAVA读写文件的例子,主要是熟悉对 4000 字符流的应用(自己学习用)
- 输出带有是/否的对话框,实现效果是:点击是,继续添加内容,点击否,返回自己定义的页面
- TORQUE引擎里如何生成一个在天空中移动的太阳(How to create a sun moving on the sky in the Torque)
- My View on the Web tech in future
- 打开一个VC工程提示"Not all of the windows in the workspace could be opened."
- 最近学习webwork+spring+hibernate,自己学习过程中的一个例子
- 未来美好憧憬:计算机之路漫漫兮,悠悠载,希望到研究生的时候能够组成一个大家自己的团队,一起学习,开发,研究...
- ORCLE 如何停止一个JOB【HOW TO STOP A JOB IN THE ORACLE?】 推荐
- Jbpm中Join节点修改后实现只要有一个分支到达流程就可以继续执行的效果
- Showing the Image file thumbnail view in ListView control using VC++ 6.0
- View statistics in the data dictionary
- 向大家请教一个关于sharepoint2003的问题,如何使一个Custom List in Datasheet View中某个field从其他list抽取数据当显示时?
- 自己学习做的一个Ajax与.net交互的例子
- static的一个心得,自己学习用