Search Unity

Editing prefab using an editor

Discussion in 'Scripting' started by unity_Cd1Hf4xikimiWA, Mar 19, 2019.

  1. unity_Cd1Hf4xikimiWA

    unity_Cd1Hf4xikimiWA

    Joined:
    Dec 1, 2018
    Posts:
    4
    Hello,
    I am trying to create an editor to edit my prefabs, I made it so that the editor can get all the variable I want, but when you edit them in the editor I made it won't apply, it just reset to the original value.
    I would like to do something like setdirty(prefab) but it doesn't work. I have no clue what to use to apply my changes.


    Code (CSharp):
    1. public Tower_Class prefab;
    2.    
    3.         public Sprite TowerIcone {
    4. get {
    5.  if (prefab != null)
    6. {
    7.  return prefab.Icone;
    8.  }
    9.  else
    10.  return null;
    11. }
    12. set
    13. {
    14.  if (prefab != null)
    15.  {
    16.  prefab.Icone = TowerIcone;  EditorUtility.SetDirty(prefab);
    17.  }
    18.  }
    19.  }
    20.  
    I don't know what I can use.
    Thanks
     
    Last edited: Mar 20, 2019
  2. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    676
    Have a look at PrefabUtility.ApplyPrefabInstance and see if that helps. Also, check out the post in this forum on using code tags. Makes your code easier to read:
    Code (CSharp):
    1. public Tower_Class prefab;
    2.  
    3. public Sprite TowerIcone
    4. {
    5.     get
    6.     {
    7.         if (prefab != null)
    8.         {
    9.             return prefab.Icone;
    10.         }
    11.         else
    12.             return null;
    13.     }
    14.  
    15.     set
    16.     {
    17.         if (prefab != null)
    18.         {
    19.             prefab.Icone = TowerIcone;
    20.  
    21.             EditorUtility.SetDirty(prefab);
    22.         }
    23.     }
    24. }
     
    unity_Cd1Hf4xikimiWA likes this.
  3. unity_Cd1Hf4xikimiWA

    unity_Cd1Hf4xikimiWA

    Joined:
    Dec 1, 2018
    Posts:
    4
    Hey,
    thanks for the quick answer :D, I will update my post to make it more readable.
    I tryed PrefabUtility.ApplyPrefabInstance but I get this error :/
    "Calling apply or revert methods on an instance which is part of a Prefab asset is not supported."
     
  4. unity_Cd1Hf4xikimiWA

    unity_Cd1Hf4xikimiWA

    Joined:
    Dec 1, 2018
    Posts:
    4
    I made it work :D thanks you guided me to the right direction :D, and some help from this : https://forum.unity.com/threads/how-to-create-a-prefab-and-set-the-materials-from-script.643732/
    my solution :
    Code (CSharp):
    1.         public Sprite TowerIcone {
    2.             get {
    3.                 if (prefab != null)
    4.                 {
    5.                     return prefab.Icone; }
    6.                 else
    7.                     return null; }
    8.             set {
    9.                 if (prefab != null)
    10.                 {
    11.                     Tower_Class prefabInstance = (Tower_Class)PrefabUtility.InstantiatePrefab(prefab);
    12.                     prefabInstance.Icone = value;
    13.                     PrefabUtility.ApplyPrefabInstance(prefabInstance.gameObject, InteractionMode.UserAction);
    14.                     AssetDatabase.SaveAssets();
    15.                     DestroyImmediate(prefabInstance.gameObject);
    16.                 }
    17.             }
    18.         }
     
    ToniGalmes and Stevens-R-Miller like this.
  5. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    676