Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 JEulerium · Feb 15, 2015 at 08:42 PM · editorprefab

Prefab generalization

Hi! I want to have one generalized prefab, with some properties (scripts) and I want to be able to automatically update the scene GameObject with properties that I set. For example, I want to have one object, in which I can select the needed sprite by some textfield (direction, for example) or so.

Of course, I can use the Start function, but I will need to start the game to see changes. Is there any possibility to see the modified prefab in editor mode (because I want to use Unity scene editor as game level editor)? Or I just need to create many prefabs?

Comment
Add comment · Show 9
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 JEulerium · Feb 15, 2015 at 12:13 PM 0
Share

Looks like [ExecuteInEdit$$anonymous$$ode] is what I want.

avatar image JEulerium · Feb 15, 2015 at 04:27 PM 0
Share

But maybe not.

avatar image meat5000 ♦ · Feb 15, 2015 at 05:22 PM 0
Share

If the prefabs contain a script which you modify, all the instances using that script will be modified too.

avatar image JEulerium · Feb 15, 2015 at 05:24 PM 0
Share

I don't understand. I mean, if I have one instantiated prefab with some properties, and I want to see the changes in Editor Window - this is the question.

avatar image JEulerium · Feb 15, 2015 at 05:25 PM 0
Share

For example, I have string field, I put "ugly" and I see, that prefab is using the ugly sprite.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Baste · Feb 16, 2015 at 11:31 PM

You can do this with a custom editor. Say you have this simple script:

 public class SpriteThingy : MonoBehaviour
 {
     public SpriteRenderer spriteRenderer;
     public string spriteChoice;
 
     public Sprite t1;
     public Sprite t2;
 }

Then you can create a custom editor that makes the spriteRenderer's sprite be dependent on what the spriteChoice string is:

 [CustomEditor(typeof(SpriteThingy))]
 public class SpriteThingyEditor : Editor
 {
     SpriteThingy script;
 
     void OnEnable()
     {
         script = (SpriteThingy)target;
     }
 
     public override void OnInspectorGUI()
     {
         script.t1 = (Sprite)EditorGUILayout.ObjectField("First sprite", script.t1, typeof(Sprite), false);
         script.t2 = (Sprite)EditorGUILayout.ObjectField("Second sprite", script.t2, typeof(Sprite), false);
 
         script.spriteRenderer = (SpriteRenderer)EditorGUILayout.ObjectField("Sprite Renderer", script.spriteRenderer, typeof(SpriteRenderer), true);
         script.spriteChoice = EditorGUILayout.TextField("Select sprite", script.spriteChoice);
 
         if (GUI.changed)
         {
             if (script.spriteChoice == "first")
                 script.spriteRenderer.sprite = script.t1;
             else if (script.spriteChoice == "second")
                 script.spriteRenderer.sprite = script.t2;
             else
                 script.spriteRenderer.sprite = null;
 
             EditorUtility.SetDirty(script);
         }
     }
 }

The example is a bit contrived - it might be more interesting to say load the correct sprite from resources or some database depending on the input string. You can also create dropdowns to let you select from custom lists, and a ton of other stuff. Custom editors are powerful.

I'll not go too much into detail on what the scripts does, but you can paste them and see that it works. Check out the docs for the Editor class. Sadly, those are only in JS (bad Unity, bad).

The short version - the editor class creates a custom view in the inspector. So it allows you to replace int fields with sliders and give stuff custom tooltips and whatever. But it also allows you to change the layout of the inspector, and change the attached object or other scripts as a reaction to what the designer does in the inspector.

The target variable is the component of the type you're looking at. OnInspectorGUI is called whenever the object is selected in the scene view. OnEnable is called when you first select it - I generally use it to cast the target variable to a variable with the correct type.

EditorGUILayout contains a bunch of auto-layouted methods. Check out the docs for the different methods for explanations.

GUI.changed is set to true whenever the gui changes. You can use that to prevent switching the sprite every frame.

EditorUtility.SetDirty(object) makes the editor save the changes to an object. This happens automatically when properties are changed through EditorGUILayout, but when I set the sprite directly, this has to be called.

Shout out if you have any questions or can't get this to work.

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.

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

Why do prefabbed meshes go missing whenever I pull an update from Unity Collab? 0 Answers

Change angle of camera in prefab preview 1 Answer

How Mark Prefab Dirty? 1 Answer

Can I force z position to match prefab when it's dragged onto scene? 1 Answer

Prefabs shown as blue boxes and not previews 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