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
1
Question by Steven-Walker · Jan 09, 2011 at 05:13 PM · editor-scriptingupdate

Is there a scene setup call or update for editor scripts?

Problem: I have an editor script that needs to perform checks within the scene, creating/deleting objects in some cases, but I am only able to get the script to execute when the object is selected (OnInspectorGUI). This is problematic however in that it requires selecting the object. I need the script to update automatically regardless of what object is selected.

Question: Is there a method or a hook I can register that will execute my script, for example upon opening a new scene? Or is there an equivalent of the Update() method for editor scripts?

Keep in mind this is all within the editor, not during runtime.

EDIT: Re-opening Question... There's one fatal flaw with using ExecuteInEditMode, and that is that when I go to build the game (for iOS in my case) I get a bunch of "Unknown Identifier" errors for EditorUtility and AssetDatabase. These are necessary for the setup I need to perform. If I move all the code into my editor script, it works great, but there's no hooks for it to be called except when the object is selected. Does anybody know a way around this?

Comment
Add comment
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

3 Replies

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

Answer by Jesus_Freak · Jan 09, 2011 at 05:18 PM

at the top of any javascript,

@script ExecuteInEditMode()

and that will tell the script to run like the game was playing. http://unity3d.com/support/documentation/ScriptReference/ExecuteInEditMode.html

Comment
Add comment · Show 1 · 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
avatar image Steven-Walker · Jan 11, 2011 at 05:07 PM 0
Share

I thought this was the answer at first, but because I need to use EditorUtility and AssetDatabase, it isn't a complete solution. Trying to use these classes, even in code that is never executed during runtime, causes "$$anonymous$$ Identifier" build errors. As far as I know there's no type of preprocessor tricks in Javascript to get around this.

avatar image
0
Best Answer

Answer by Steven-Walker · Jan 14, 2011 at 12:02 AM

I was able to work-around this problem using a C# utility file as an interface to the editor functions I need. Since C# supports the preprocessor and Javascript does not, I used it to create functions that strip out functionality at build time.

Below is a simplified version of the script I created:

using System; using UnityEngine; #if UNITY_EDITOR using UnityEditor; #endif using System.Collections;

public class EditorSetupUtility {

 public static UnityEngine.Object LoadAssetAtPath(string assetPath, Type type)
 {
     UnityEngine.Object asset = null;

if UNITY_EDITOR

    asset = AssetDatabase.LoadAssetAtPath(assetPath, type);

endif

    return asset;
 }

}

And then in my runtime script I'd do something like this:

function Update() {
    if(Application.isEditor && Application.isPlaying) {
        var framePrefab : GameObject = EditorSetupUtility.LoadAssetAtPath("Assets/Prefabs/Frame.prefab", typeof(GameObject)) as GameObject;
        // .. do other stuff ..
    }
}
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
avatar image
0

Answer by Vermelhu · Jun 12, 2011 at 11:56 AM

You can use the #if UNITY_EDITOR preprocessor tags in javascript, in case anyone's wondering :)Here's some code I wrote to allow resizing over cover points in the inspector in realtime:

 @script ExecuteInEditMode()
 ...
 ...
 #if UNITY_EDITOR
 function Update() {
     if (gameObject.transform.localScale.x != width)
         gameObject.transform.localScale.x = width;
     
     
     if (lowCover && gameObject.transform.localScale.y != lowCoverHeight) {
         gameObject.transform.localScale.y = lowCoverHeight;
         gameObject.transform.position.y = 
             gameObject.transform.parent.position.y + (lowCoverHeight/2);
     }
     else if (!lowCover && gameObject.transform.localScale.y != highCoverHeight) {
         gameObject.transform.localScale.y = highCoverHeight;
         gameObject.transform.position.y = 
             gameObject.transform.parent.position.y + (highCoverHeight/2);
     }
 }
 #endif
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

1 Person is following this question.

avatar image

Related Questions

RectTransform.rect.width/height Incorrect On Editor Startup 1 Answer

How to assign multiple EditorApplication.update delegates in JS? 2 Answers

When is Update called in edit mode? 3 Answers

reconnect programmatically instantiated prefab 1 Answer

How do I create a new object in the Editor as a child of another object? 1 Answer


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