[cb]Unity 關卡編輯器 開發


關卡編輯器

關卡編輯器插件開發日記

1. Apply to Prefab [替換Prefab]

image

if (GUILayout.Button("Apply Collider To Prefab"))
            {
                PrefabUtility.ReplacePrefab(simActor.Preview, PrefabUtility.GetPrefabParent(simActor.Preview), ReplacePrefabOptions.ConnectToPrefab);
            }

 

2. 獲取Scene的中間坐標

比如每次 New Actor時,都出現在Scene視圖的中間

   SceneView.onSceneGUIDelegate -= OnCustomSceneGUI;
    void OnCustomSceneGUI(SceneView sceneview)
    {
        SceneViewPos = sceneview.pivot;
    }
//創建Actor
    public void CreateMapActor()
   {
        GameObject gameLogic = GameObject.Find("MapLogic");
        GameObject newActor = GameObject.CreatePrimitive(PrimitiveType.Sphere);

        newActor.name = "Actor-" + UnityEngine.Random.Range(1, 999999);
        CBaseTool.SetChild(newActor.transform, gameLogic.transform);
        Selection.activeGameObject = newActor;
        CSimActor simActor = newActor.AddComponent<CSimActor>();

        newActor.transform.position = SceneViewPos;
    }

 

3. Scene Context Menu[場景視圖添加右鍵菜單]

可以參考NGUI的 UIWidgetContainerEditor. NGUIEditorTools.ShowSpriteSelectionMenu(e.mousePosition); 我這兒實現的,還沒有做處理

using UnityEngine;
using UnityEditor;

[InitializeOnLoad]
[ExecuteInEditMode]
public class MySceneContext : MonoBehaviour
{

    void Update()
    {
        SceneView.onSceneGUIDelegate = SceneContext;
    }

    void SceneContext(SceneView sceneview)
    {
        if (Selection.activeTransform == null) return;
        Transform selectTrans = Selection.activeTransform;
        Vector3 curPos = selectTrans.position;

        Event evt = Event.current;
        if (evt.type == EventType.ContextClick)
        {
            GenericMenu menu = new GenericMenu();
            menu.AddItem(new GUIContent("MenuItem1"), false, CallBack, "item 1");
            menu.AddItem(new GUIContent("MenuItem2"), false, CallBack, "item 2");
            menu.ShowAsContext();
            evt.Use();
        }
    }

    void CallBack(object userData)
    {

    }
}

4. Inspector Context Menu

image

[MenuItem("CONTEXT/Transform/MyContext1")]
    public static void MyContext(MenuCommand command)
    {
        CBase.Log("context menu");
    }

文獻資料

http://docs.unity3d.com/ScriptReference/MenuCommand-context.html

http://answers.unity3d.com/questions/22947/adding-to-the-context-menu-of-the-hierarchy-tab.html

 

The CONTEXT/{string} seems to work for components within the Inspector

同時可查看 NGUI\Editor\NGUIContextMenu.cs

可選插件

Asset Store工具推薦:https://www.assetstore.unity3d.com/en/#!/content/10424


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM