如何通过脚本在UnityEditor中的预制件上编辑MonoBehaviour的属性 [英] How to edit a MonoBehaviour's properties on a prefab in the UnityEditor from Script

查看:554
本文介绍了如何通过脚本在UnityEditor中的预制件上编辑MonoBehaviour的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个简单的MonoBehaviour

public class FooScript : MonoBehaviour {
  public int someValue = 0;
}

我制作GameObject,附加一个FooScript.我用它做一个预制件,然后在场景中实例删除.

I make GameObject, attach a FooScript. I make a prefab from that and delete in instance in the scene.

在编辑器中,我可以选择项目中的预制件,也可以编辑someValue.

In the editor I can select the prefab in the project and I can edit someValue.

假设我知道预制件的路径,如何制作一个编辑时脚本来对预制件中的FooScript实例进行编辑?

Assuming I know the path to the prefab how I can make an edit time script that makes edits to the FooScript instance inside the prefab?

尝试了什么

GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(pathToPrefab);
GameObject root = ???? // WHAT DO I CALL HERE!?
FooScript fooScript = root.GetComponent<FooScript>();
fooScript.someValue = 789;

实际上,在这种情况下,prefab为空

Actually in that case prefab is null

如果切换到

Object prefab = AssetDatabase.LoadMainAssetAtPath(prefabPath);                 
GameObject root = prefab as GameObject;

prefab不为空,但root

接下来我尝试了这个

Object[] objs = AssetDatabase.LoadAllAssetsAtPath(prefabPath);
foreach (var o in obs)
{
   Debug.Log("    type:" + (o.GetType().Name));
}

打印

type: GameObject
type: Transform
type: FooScript

但是使用该解决方案很麻烦,因为我必须尝试每个GameObject和/或FooScript.我的意思是我想作为一个务实的解决方案可能会奏效,但感觉就像增加了技术债务,我稍后必须修复

But using that is a hacky solution as I'd have to try every GameObject and or FooScript. I means I suppose as a pragmatic solution it might work but it feels like adding technical debt I'll have to fix later

我尝试过的其他方法是使用新的GameObject和新的FooScript创建一个新的预制件,并调用PrefabUtility.ReplacePrefab

Other things I've tried is creating a new prefab with a new GameObject and a new FooScript and calling PrefabUtility.ReplacePrefab

GameObject newGameObject = new GameObject();
FooScript fooScript = newGameObject.AddComponent<FooScript>();
fooScript.someValue = 789;

Object prefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
PrefabUtility.ReplacePrefab(newGameObject, prefab, ReplacePrefabOptions.ConnectToPrefab);

GameObject.Destory(newGameObject); // don't want this in the scene

但这对我不起作用.预制中的值将被替换,但场景中的所有实例都将断开连接.

But that's not working for me. The values in the prefab get replaced but all instances in the scene get disconnected.

重申.我不是要编辑预制实例.我正在尝试在编辑时通过脚本编辑预制件本身.

To reiterate. I'm NOT trying to edit an instance of a prefab. I'm trying to edit the prefab itself from a script at edit time.

推荐答案

 // this fails
 GameObject prefab = 
    AssetDatabase.LoadAssetAtPath<GameObject>(pathToPrefab));

 // this fails
 GameObject prefab = 
    AssetDatabase.LoadAssetAtPath(pathToPrefab, typeof(GameObject));  

 // this succeeds
 GameObject prefab = 
    AssetDatabase.LoadAssetAtPath(pathToPrefab, typeof(Object)) as GameObject;

这篇关于如何通过脚本在UnityEditor中的预制件上编辑MonoBehaviour的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆