0
点赞
收藏
分享

微信扫一扫

代码修改预制

Villagers 2022-01-23 阅读 81
				publiv void ModifyPrefab(){//代码修改预制信息并保存
					string pre_path = GetPrefabPath(gameObject);
                GameObject obj = PrefabUtility.LoadPrefabContents(pre_path);//通过路径加载预制
                //StageTimeline stage_script = obj.GetComponent<StageTimeline>();//修改预制信息
                //stage_script.m_PuppetList = obj.transform.GetComponentsInChildren<PuppetMaster>(true);
                PrefabUtility.SaveAsPrefabAsset(obj,pre_path);//将修改后的预制保存到项目
                PrefabUtility.UnloadPrefabContents(obj);//卸载预制
			}
                
    public string GetPrefabPath(GameObject gameObject) {//获取项目中预制体的路径
		#if UNITY_EDITOR
            // Project中的Prefab是Asset不是Instance
            if (UnityEditor.PrefabUtility.IsPartOfPrefabAsset(gameObject))
            {
                // 预制体资源就是自身
                return UnityEditor.AssetDatabase.GetAssetPath(gameObject);
            }

            // Scene中的Prefab Instance是Instance不是Asset
            if (UnityEditor.PrefabUtility.IsPartOfPrefabInstance(gameObject))
            {
                // 获取预制体资源
                var prefabAsset = UnityEditor.PrefabUtility.GetCorrespondingObjectFromOriginalSource(gameObject);
                return UnityEditor.AssetDatabase.GetAssetPath(prefabAsset);
            }
		#endif
        return null;
    }
举报

相关推荐

0 条评论