• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sparrow · Jun 26, 2012 at 12:54 AM · customeditor

Custom editor for prefabs

Hi!

I have a customEditor script for component called "Upgradeable" that displays array of data differently in inspector. Now while this approach is working fine on scene objects (I can modify data via my customEditor) I can not get it to work on non-instantiated prefabs. It seems that the data just does not get saved.

Here is my code:

#pragma strict @CustomEditor(Upgradeable) class UpgradeableEditor extends Editor{ private var m_Object : SerializedObject; private var m_upgradesCount : SerializedProperty; private static var upgradesSize : String = "upgrades.Array.size"; private static var upgradesData : String = "upgrades.Array.data[{0}]"; function GetUpgradesArray(){ this.m_upgradesCount = this.m_Object.FindProperty(upgradesSize); var upgradesArray : Upgrade[] = new Upgrade[this.m_upgradesCount.intValue]; for(var i = 0; i < this.m_upgradesCount.intValue; i++){ upgradesArray[i] = m_Object.FindProperty(String.Format(upgradesData, i)).objectReferenceValue as Upgrade; } return upgradesArray; } function SetUpgrade(index : int, upgrade : Upgrade){ m_Object.FindProperty(String.Format(upgradesData, index)).objectReferenceValue = upgrade as UnityEngine.Object; } function AddUpgrade(){ this.m_upgradesCount.intValue++; var upgrade : Upgrade = Upgrade.CreateInstance(typeof(Upgrade)); upgrade.name = "Upgrade" + this.m_upgradesCount.intValue; this.SetUpgrade(this.m_upgradesCount.intValue -1, upgrade); } function GetUpgradeAt(index : int){ return m_Object.FindProperty(String.Format(upgradesData, index)).objectReferenceValue as Upgrade; } function RemoveUpgradeAt(index : int){ for (var i : int = index; i < this.m_upgradesCount.intValue - 1; i++) SetUpgrade(i, GetUpgradeAt(i+1)); this.m_upgradesCount.intValue--; } function OnEnable(){ m_Object = new SerializedObject(target); } //TODO: check that this function gets executed only in debug mode. Don't want this going on in production function OnInspectorGUI () { m_Object.Update(); GUILayout.Label("Upgrades", EditorStyles.boldLabel); var upgrades : Upgrade[] = GetUpgradesArray(); for(var i = 0; i < upgrades.Length; i++){ Debug.Log(upgrades[0]); var upgrade : Upgrade = upgrades[i]; GUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Price"); upgrade.price = EditorGUILayout.IntField(upgrade.price); EditorGUILayout.LabelField("Object"); upgrade.upgradObject = EditorGUILayout.ObjectField(upgrade.upgradObject, GameObject); if(GUILayout.Button("Remove", GUILayout.Width(70))) RemoveUpgradeAt(i); GUILayout.EndHorizontal(); EditorGUILayout.Space(); if(GUI.changed) SetUpgrade(i, upgrade); } if(GUILayout.Button("Add Upgrade")){ AddUpgrade(); } m_Object.ApplyModifiedProperties(); } }

Looks like that "AddUpgrade" function is not saving data so the GetUpgradesArray function can not fetch it. Again I repeat this is working fine if that prefab is instantiated in the scene view. Any ideas how to save data in prefab objects?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JeanSimonet · Sep 27, 2013 at 05:39 PM 0
Share

I'm curious if you ever found an answer to this. I am running into the same issue myself trying to implement PropertyDrawers.

avatar image sparrow · Sep 30, 2013 at 10:22 AM 0
Share

If I remember correctly my problem was not understanding how exactly prefabs are working in Unity. I have found a way around this problem, but I think a better solution would be: - instantiate a prefab you want to change - make changes to that object - Save that object as a new prefab (how to achieve this explained here)

Hope this helps

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Jamora · Oct 07, 2013 at 08:13 PM

Because you're working with a prefab, it means it's stored on the disk. Any changes you make should also be stored to the disk.

You most likely need to check if the current target is a prefab stored on the disk or an instantiated object with EditorUtility.IsPersistent; if it's a prefab then call EditorUtility.SetDirty so Unity knows the object has changed. Then either let the user save manually or do AssetDatabase.SaveAssets in OnDisable.. or somewhere.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Custom Editor item list 1 Answer

Custom inspector: function for assets? 3 Answers

GUILayoutUtility GetRect with string parameter? 0 Answers

How to prevent Editor script variables resetting at runtime 2 Answers

Custom Editors for any Component 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges