继续学习之Gizmos in the Scene View的自己写的一个无聊的效果
来源:互联网 发布:linux vim 查找 编辑:程序博客网 时间:2024/06/10 15:17
说明:在 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》,如有错误请告知
0 0
- 继续学习之Gizmos in the Scene View的自己写的一个无聊的效果
- 周末闲着无聊分享一个自己写的带呼吸效果的android水波纹自定义view
- 无聊写一个自己接入微信支付的文章
- 无聊的培训继续
- 大光头的shader学习之路-自己写的一个消融效果
- 写个自己看的,很无聊
- 自己写的一个分页效果
- Unity编辑器扩展之Gizmos的学习(线框)
- 无聊写的无聊东西
- 无聊写的一个记事簿APP
- 无聊写的 一个Soeckt 客户端例子
- 无聊写的
- 无聊时候写的。。。
- 无聊写的压位高精加
- There are 2 audio listeners in the scene.的问题
- 自己写的一个div随页面滚动的效果
- 自己写的一个渐变view,点击后可缩放
- 闲来无聊,自己写的itoa函数
- HOJ 2201 题解
- 安装caffe(CPU版)遇到的问题
- hibernate之constrained详解
- 文章标题 POJ 1151 : Atlantis (线段树+扫描线)
- 有感而发
- 继续学习之Gizmos in the Scene View的自己写的一个无聊的效果
- js Boolean函数和相等操作符规则
- 1082.Read Number in Chinese (25)...to be continued...
- HashMap与HashTable的区别
- Eclipse中执行Maven命令时控制台输出乱码(全)
- 文件转换为utf-8编码(python小脚本)
- 无脑博士的试管们
- 程序设计之流程控制
- 使用Android studio所遇到的问题