标签:csdn value archive hive .com int 设置 range 1.5
using UnityEngine;
using System.Collections; public class EnemySpawn : MonoBehaviour { public float range; void OnDrawGizmos() { Gizmos.color = Color.green; Gizmos.DrawWireCube(transform.position, new Vector3(range, 0, range)); } void OnDrawGizmosSelected() { Gizmos.color = Color.yellow; Gizmos.DrawWireCube(transform.position, new Vector3(range, 0, range)); } } |
例如以下图所看到的:
using UnityEngine;
using UnityEditor; [CustomEditor(typeof(EnemySpawn))] public class EnemySpawnEditor : Editor { void OnSceneGUI() { EnemySpawn enemySpawn = target as EnemySpawn; Vector3 pos = enemySpawn.transform.position; // 计算4个角落 Vector3 v0 = new Vector3(pos.x - enemySpawn.range / 2, pos.y, pos.z - enemySpawn.range / 2); Vector3 v1 = new Vector3(pos.x + enemySpawn.range / 2, pos.y, pos.z + enemySpawn.range / 2); float y = v0.y; Vector3[] corners = new Vector3[4]; corners[0] = new Vector3(v0.x, y, v0.z); corners[1] = new Vector3(v0.x, y, v1.z); corners[2] = new Vector3(v1.x, y, v1.z); corners[3] = new Vector3(v1.x, y, v0.z); // 4个缩放值控制柄 for (int i = 0; i < 4; ++i) { enemySpawn.range = Handles.ScaleValueHandle(enemySpawn.range, corners[i], Quaternion.identity, 1, Handles.SphereCap, 1); Handles.Label(corners[i], i.ToString()); } if (GUI.changed) { EditorUtility.SetDirty(target); } } } |
using UnityEngine;
using UnityEditor; [CustomEditor(typeof(EnemySpawn))] public class EnemySpawnEditor : Editor { void OnSceneGUI() { EnemySpawn enemySpawn = target as EnemySpawn; Vector3 pos = enemySpawn.transform.position; // 计算4个角落 Vector3 v0 = new Vector3(pos.x - enemySpawn.range / 2, pos.y, pos.z - enemySpawn.range / 2); Vector3 v1 = new Vector3(pos.x + enemySpawn.range / 2, pos.y, pos.z + enemySpawn.range / 2); float y = v0.y; Vector3[] corners = new Vector3[4]; corners[0] = new Vector3(v0.x, y, v0.z); corners[1] = new Vector3(v0.x, y, v1.z); corners[2] = new Vector3(v1.x, y, v1.z); corners[3] = new Vector3(v1.x, y, v0.z); EditorGUI.BeginChangeCheck(); float scaleValue = enemySpawn.range; float newRange = scaleValue; // 4个缩放值控制柄 for (int i = 0; i < 4; ++i) { scaleValue = Handles.ScaleValueHandle(enemySpawn.range, corners[i], Quaternion.identity, 1, Handles.SphereCap, 1); newRange = scaleValue == enemySpawn.range ? newRange : scaleValue; Handles.Label(corners[i], i.ToString()); } if (EditorGUI.EndChangeCheck()) { // 记录对象以便进行撤销 Undo.RecordObject(enemySpawn, "Change Range"); enemySpawn.range = newRange; EditorUtility.SetDirty(target); } } } |
using UnityEngine;
using UnityEditor; [CustomEditor(typeof(EnemySpawn))] public class EnemySpawnEditor : Editor { void OnSceneGUI() { EnemySpawn enemySpawn = target as EnemySpawn; Vector3 pos = enemySpawn.transform.position; // 计算4个角落 Vector3 v0 = new Vector3(pos.x - enemySpawn.range / 2, pos.y, pos.z - enemySpawn.range / 2); Vector3 v1 = new Vector3(pos.x + enemySpawn.range / 2, pos.y, pos.z + enemySpawn.range / 2); float y = v0.y; Vector3[] corners = new Vector3[4]; corners[0] = new Vector3(v0.x, y, v0.z); corners[1] = new Vector3(v0.x, y, v1.z); corners[2] = new Vector3(v1.x, y, v1.z); corners[3] = new Vector3(v1.x, y, v0.z); EditorGUI.BeginChangeCheck(); float scaleValue = enemySpawn.range; float newRange = scaleValue; float sizeHandle = 1.0f; float realSize = sizeHandle * 0.1f; // 实际为0.15f,比它小才干更精确显示鼠标 // 4个缩放值控制柄 for (int i = 0; i < 4; ++i) { v0 = new Vector3(corners[i].x - realSize, y, corners[i].z - realSize); v1 = new Vector3(corners[i].x + realSize, y, corners[i].z + realSize); // 换算成GUI坐标 Vector2[] handles = new Vector2[4]; handles[0] = HandleUtility.WorldToGUIPoint(new Vector3(v0.x, y, v0.z)); handles[1] = HandleUtility.WorldToGUIPoint(new Vector3(v0.x, y, v1.z)); handles[2] = HandleUtility.WorldToGUIPoint(new Vector3(v1.x, y, v1.z)); handles[3] = HandleUtility.WorldToGUIPoint(new Vector3(v1.x, y, v0.z)); Bounds b = new Bounds(handles[0], Vector3.zero); for (int j = 1; j < 4; ++j) { b.Encapsulate(handles[j]); } Vector2 min = b.min; Vector2 max = b.max; Rect rect = new Rect(min.x, min.y, max.x - min.x, max.y - min.y); // 调试绘制是否计算正确 Handles.BeginGUI(); GUI.Box(rect, rect.ToString()); Handles.EndGUI(); // 是否鼠标在控制柄上 if (rect.Contains(Event.current.mousePosition)) { // 显示缩放图标的箭头 EditorGUIUtility.AddCursorRect(rect, MouseCursor.ScaleArrow); } scaleValue = Handles.ScaleValueHandle(enemySpawn.range, corners[i], Quaternion.identity, sizeHandle, Handles.SphereCap, 1); newRange = scaleValue == enemySpawn.range ? newRange : scaleValue; |
如今能够在场景视图看到每一个球体都有一个矩形包围着。当鼠标进入到这个矩形里面。就会改变鼠标图形,例如以下图所看到的:
// 调试绘制是否计算正确
Handles.BeginGUI(); GUI.Box(rect, rect.ToString()); Handles.EndGUI(); |
标签:csdn value archive hive .com int 设置 range 1.5
原文地址:http://www.cnblogs.com/slgkaifa/p/6928926.html