【Unity編輯器】Case:給選中對象添加組件,實例化對象

[MenuItem("Window/UnitZ/Component/Character/Player")]
    static void CreatePlayer ()
    {
        if (Selection.activeGameObject != null) {
            if (Selection.activeGameObject.GetComponent<Animator> () == null) {
                Debug.LogWarning ("The model must have 'Animator' component");
            }

            Selection.activeGameObject.AddComponent<PlayerCharacter> ();

            Object prefab = AssetDatabase.LoadAssetAtPath ("Assets/UnitZ/Editor/Prefabs/FPScamera.prefab", typeof(GameObject));
            GameObject clone = Instantiate (prefab, Vector3.zero, Quaternion.identity) as GameObject;
            clone.transform.SetParent (Selection.activeGameObject.transform);
            clone.transform.position = Vector3.zero;
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章