该工具主要用来快捷的存储场景中物体的多个位置信息,并快捷的切换到相应位置(用于相机位置切换及其他物体位置的切换)。效果如下:
首先在Hierarchy视图框中右键创建“位置信息管理对象”
接着添加管理对象,将场景中物体拖入指定位置。
效果如下:
具体实现代码如下:
using System.Collections.Generic;
using UnityEngine;
[DisallowMultipleComponent]
public class MyPosition:MonoBehaviour
{
private static MyPosition _instance = null;
public static MyPosition Instance
{
get
{
if (_instance == null)
{
_instance = (new GameObject("位置信息管理")).AddComponent<MyPosition>();
}
return _instance;
}
}
private void Awake()
{
_instance = this;
}
public List<PositionObject> _list = new List<PositionObject>();
[System.Serializable]
public class PositionObject
{
public bool _show = true;
public GameObject _obj;
public List<TransformInformation> _list = new List<TransformInformation>();
}
/// <summary>
/// 获取信息
/// </summary>
/// <param name="obj"></param>
/// <param name="desc"></param>
/// <returns></returns>
public TransformInformation GetInformation(GameObject obj, string desc)
{
for (int i = 0; i < _list.Count; i++)
{
if (_list[i]._obj == obj)
{
for (int j = 0; j < _list[i]._list.Count; j++)
{
if (_list[i]._list[j]._desc == desc) return _list[i]._list[j];
}
}
}
return null;
}
/// <summary>
/// 获取局部位置坐标
/// </summary>
/// <param name="obj"></param>
/// <param name="desc"></param>
/// <returns></returns>
public Vector3 GetLocalPosition(GameObject obj, string desc)
{
return GetInformation(obj, desc)._localPosition;
}
/// <summary>
/// 获取世界位置坐标
/// </summary>
/// <param name="obj"></param>
/// <param name="desc"></param>
/// <returns></returns>
public Vector3 GetPosition(GameObject obj, string desc)
{
return GetInformation(obj, desc)._position;
}
/// <summary>
/// 获取旋转角度
/// </summary>
/// <param name="obj"></param>
/// <param name="desc"></param>
/// <returns></returns>
public Quaternion GetRotation(GameObject obj, string desc)
{
return GetInformation(obj, desc)._rotation;
}
/// <summary>
/// 获取缩放大小
/// </summary>
/// <param name="obj"></param>
/// <param name="desc"></param>
/// <returns></returns>
public Vector3 GetScale(GameObject obj, string desc)
{
return GetInformation(obj, desc)._localScale;
}
/// <summary>
/// 设置信息
/// </summary>
/// <param name="obj"></param>
/// <param name="desc"></param>
/// <returns></returns>
public TransformInformation SetInformation(GameObject obj, string desc)
{
TransformInformation information = GetInformation(obj, desc);
obj.GetComponent<Transform>().localPosition = information._localPosition;
obj.GetComponent<Transform>().rotation = information._rotation;
obj.GetComponent<Transform>().localScale = information._localScale;
return information;
}
/// <summary>
/// 设置局部位置坐标
/// </summary>
/// <param name="obj"></param>
/// <param name="desc"></param>
/// <returns></returns>
public TransformInformation SetLocalPosition(GameObject obj, string desc)
{
TransformInformation information = GetInformation(obj, desc);
obj.GetComponent<Transform>().localPosition = information._localPosition;
return information;
}
/// <summary>
/// 设置世界位置坐标
/// </summary>
/// <param name="obj"></param>
/// <param name="desc"></param>
/// <returns></returns>
public TransformInformation SetPosition(GameObject obj, string desc)
{
TransformInformation information = GetInformation(obj, desc);
obj.GetComponent<Transform>().position = information._position;
return information;
}
/// <summary>
/// 设置旋转角度
/// </summary>
/// <param name="obj"></param>
/// <param name="desc"></param>
/// <returns></returns>
public TransformInformation SetRotation(GameObject obj, string desc)
{
TransformInformation information = GetInformation(obj, desc);
obj.GetComponent<Transform>().rotation = information._rotation;
return information;
}
/// <summary>
/// 设置缩放大小
/// </summary>
/// <param name="obj"></param>
/// <param name="desc"></param>
/// <returns></returns>
public TransformInformation SetScale(GameObject obj, string desc)
{
TransformInformation information = GetInformation(obj, desc);
obj.GetComponent<Transform>().localScale = information._localScale;
return information;
}
}
[System.Serializable]
public class TransformInformation
{
public string _desc = "描述";
public bool _show = false;
public Vector3 _localPosition;
public Vector3 _position;
public Quaternion _rotation;
public Vector3 _localScale;
public Vector3 _scale;
}
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
[CustomEditor(typeof(MyPosition))]
public class MyPositionEditor : Editor
{
private MyPosition _myPosition;
private MyPosition.PositionObject _positionObject;
private TransformInformation _information;
private string _flodOutName;
private int _deleteObjectIndex = -1;
private int _deletePositionIndex = -1;
public override void OnInspectorGUI()
{
serializedObject.Update();
_myPosition = target as MyPosition;
EditorGUILayout.LabelField("拖动场景对象到下面任意区域");
GUILayout.Space(30);
_deleteObjectIndex = -1;
_deletePositionIndex = -1;
for (int i = 0; i < _myPosition._list.Count; i++)
{
_positionObject = _myPosition._list[i];
_flodOutName = "待赋值";
if (_positionObject._obj != null) _flodOutName = _positionObject._obj.name;
EditorGUILayout.BeginHorizontal();
_positionObject._show = EditorGUILayout.Foldout(_positionObject._show, _flodOutName);
if (GUILayout.Button("删除"))
{
if (EditorUtility.DisplayDialog("警告", "你确定要删除该对象吗?", "确定", "取消"))
{
_deleteObjectIndex = i;
}
}
EditorGUILayout.EndHorizontal();
if (_positionObject._show)
{
EditorGUILayout.BeginHorizontal();
_myPosition._list[i]._obj = (GameObject)EditorGUILayout.ObjectField(_myPosition._list[i]._obj, typeof(GameObject), true);
if (GUILayout.Button("添加位置"))
{
_positionObject._list.Add(new TransformInformation());
SavePosition(_positionObject._list[_positionObject._list.Count - 1], _positionObject._obj.GetComponent<Transform>());
}
EditorGUILayout.EndHorizontal();
for (int j = 0; j < _positionObject._list.Count; j++)
{
_information = _positionObject._list[j];
EditorGUILayout.BeginHorizontal();
_information._desc = EditorGUILayout.TextField(_information._desc);
if (GUILayout.Button("切换到该位置"))
{
Transform transform = _positionObject._obj.GetComponent<Transform>();
transform.localPosition = _information._localPosition;
transform.rotation = _information._rotation;
transform.localScale = _information._localScale;
}
if (GUILayout.Button("保存当前位置"))
{
SavePosition(_information, _positionObject._obj.GetComponent<Transform>());
}
if (GUILayout.Button("删除"))
{
if (EditorUtility.DisplayDialog("警告", "你确定要删除当前保存的位置信息吗?", "确定", "取消"))
{
_deletePositionIndex = j;
}
}
EditorGUILayout.EndHorizontal();
GUILayout.Space(20);
}
if (_deletePositionIndex != -1) _positionObject._list.RemoveAt(_deletePositionIndex);
}
GUILayout.Space(50);
}
if (_deleteObjectIndex != -1) _myPosition._list.RemoveAt(_deleteObjectIndex);
if (Event.current.type == EventType.DragExited)
{
if (DragAndDrop.objectReferences.Length != 0)
{
for (int i = 0; i < DragAndDrop.objectReferences.Length; i++)
{
if (DragAndDrop.objectReferences[i].GetType() == typeof(GameObject))
{
if (IsExistGameObject(_myPosition, (GameObject)DragAndDrop.objectReferences[i]) == false)
{
_positionObject = new MyPosition.PositionObject();
_positionObject._obj = (GameObject)DragAndDrop.objectReferences[i];
_myPosition._list.Add(_positionObject);
}
else
{
Debug.LogError("列表已存在该对象");
}
}
}
}
}
GUILayout.Space(1000);
serializedObject.ApplyModifiedProperties();
EditorUtility.SetDirty(_myPosition);
if (GUI.changed) EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
}
/// <summary>
/// 是否存在该对象
/// </summary>
/// <param name="myPosition"></param>
/// <param name="obj"></param>
/// <returns></returns>
public bool IsExistGameObject(MyPosition myPosition,GameObject obj)
{
for (int i = 0; i < myPosition._list.Count; i++)
{
if (myPosition._list[i]._obj == obj)
{
return true;
}
}
return false;
}
/// <summary>
/// 保存位置
/// </summary>
/// <param name="information"></param>
/// <param name="transform"></param>
private void SavePosition(TransformInformation information, Transform transform)
{
information._localPosition = transform.localPosition;
information._position = transform.position;
information._rotation = transform.rotation;
information._localScale = transform.localScale;
}
[MenuItem("GameObject/自定义/创建位置信息管理对象", false, MenuItemConfig.位置信息管理)]
private static void CreateSoundControlObject()
{
GameObject gameObject = new GameObject("位置信息管理");
gameObject.AddComponent<MyPosition>();
EditorUtility.FocusProjectWindow();
Selection.activeObject = gameObject;
EditorGUIUtility.PingObject(Selection.activeObject);
Undo.RegisterCreatedObjectUndo(gameObject, "Create GameObject");
EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
}
}
使用代码快速切换到相应位置
MyPosition.Instance.SetInformation(gameObject, "左边位置");