Editor-物体位置信息存储

该工具主要用来快捷的存储场景中物体的多个位置信息,并快捷的切换到相应位置(用于相机位置切换及其他物体位置的切换)。效果如下:
首先在Hierarchy视图框中右键创建“位置信息管理对象”


2978185-d24d96b2ec9408b3.png
321.png

接着添加管理对象,将场景中物体拖入指定位置。
效果如下:

343.png

具体实现代码如下:

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, "左边位置");
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,590评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 86,808评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,151评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,779评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,773评论 5 367
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,656评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,022评论 3 398
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,678评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,038评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,659评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,756评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,411评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,005评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,973评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,053评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,495评论 2 343

推荐阅读更多精彩内容