Hello, everyone.
Here is the problem I met:
I wrote a editor window (which extends from EditorWindow class), which can change variables of a specified script component.
When the script component is attached to a game object which do not link with a prefab, it works fine. But when the game object have a prefab in Asset Folder, it will reset to the prefab state every few moment or when I start to play the game.
Since when I change variable of same game object from inspector works fine, I guess the reason of this problem might because Unity3D do not receive such changing I did from the editor window when the game object have linking with a prefab.
I already found a solution but am not sure if this is the best solution. Any input would be great.
Below is part of my code of editor window:
function OnGUI () {
//Check Object
if(Selection.gameObjects.length > 1 || Selection.gameObjects.length <= 0)
{
return;
}
//Check Component
var current_obj : GameObject = Selection.gameObjects[0];
var ai_simple : AI_Simple = current_obj.GetComponent(AI_Simple);
if( !ai_simple )
{
return;
}
//Chang Variable example
if(GUI.Button(rect, "AddnewAI-Mode"))
{
ai_simple.AddMode(); //Add a new element into the array
}
And to solve the problem, I added these codes after “Check Component” part:
if(PrefabUtility.GetPrefabType(current_obj) == PrefabType.PrefabInstance)
{
PrefabUtility.DisconnectPrefabInstance(current_obj);
}
It can still recover prefab state by pressing “Revert” button, I’m not sure this is a good solution or not , please help me, thank you.