• 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
Question by Seth-Bergman · Jan 10, 2013 at 04:25 PM · editoreditor-scriptinggenericdynamic loading

Editor Script: Dynamically reference a specific script/data member via user input

Ok, hello all, basically I have a game where I am using lots of image stacks as sprites, so in order to avoid having to painstakingly drag-n-drop images into arrays by hand, I have a small editor script to do that for me. When I have a new stack, I put the folder in the Resources folder, then run the following:

 @script ExecuteInEditMode()
 
 class LoadImageStack extends EditorWindow {
 
 var pathOfFolder : String = "Textures/";
 var nameOfArray : String;     //I'd like to use t$$anonymous$$s
 var targetScript : String;    //and t$$anonymous$$s for the LoadAll function
 var target : GameObject;
 
     @MenuItem ("Window/Load Image Stack")
     static function ShowWindow () {
         EditorWindow.GetWindow (LoadImageStack);
     }
 
     function OnGUI () {
     
     pathOfFolder = EditorGUILayout.TextField("Path: ",pathOfFolder);
     nameOfArray = EditorGUILayout.TextField("Name of array: ",nameOfArray);
     targetScript = EditorGUILayout.TextField("Name of Script: ",targetScript);
             EditorGUILayout.BeginHorizontal();
     target = EditorGUILayout.ObjectField(target,GameObject,true);
             EditorGUILayout.EndHorizontal();
         
              if (GUILayout.Button("LoadNow!"))
         {
             LoadAll();
         }
     }
     
     function LoadAll()
    {
      var targetArray = Resources.LoadAll(pathOfFolder, Texture2D);
      var scriptToSet : JonsTramp = target.GetComponent(JonsTramp);  //QUESTION 1
      scriptToSet.myTextures = new Texture[targetArray.length];   //QUESTION 2
     for (var i = 0; i < targetArray.length;i++){
              scriptToSet.myTextures[i] = targetArray[i];
        }
    }
 }

t$$anonymous$$s works great, and once I load the images, I can even drag the folder out of Resources and into the pics folder where it belongs, without breaking the connection..

My problem is, I have to manually edit the script to put the name of the target script in at the appropriate line (JonsTramp, QUESTION 1 above). And then I have to manually edit the name of the array variable as well (myTextures, QUESTION 2 above).

QUESTION 1: I know I can use the generic version of GetComponent:

      var scriptToSet = target.GetComponent(targetScript); 

t$$anonymous$$s would allow the user to type in the name of the script on the fly, w$$anonymous$$ch would be much better than my current method. But then how could I get from an "Object" to a variable on that object? Is it possible?

Question 2: Same t$$anonymous$$ng with the var, how can I let the user select it?

I don't want to list off all possible names of scripts in advance, I'd like for the user to be able to point to the appropriate components, and have it all just work. I tried using strings, as seen above, but I just don't know how to go to a variable via it's name as a string..

if I could dynamically check the given script for ALL ARRAYS, then present these to the user to choose from, that would be even better..

(ultimately I would want to do the same t$$anonymous$$ng with choosing the script, but I'm pretty sure I could get that part working, using GetComponents, then listing off the names, but of course that leaves me with a string as well)

I appreciate any help!

Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by numberkruncher · Jan 10, 2013 at 05:25 PM

I do not generally use UnityScript so the following may not be entirely correct. Please comment if t$$anonymous$$s does not work for you and I will take a closer look for you.

QUESTION 1: I know I can use the generic version of GetComponent:

 var scriptToSet = ( targetScript != null )
     ? target.GetComponent(targetScript.GetClass())
     : null;

Question 2: Same t$$anonymous$$ng with the var, how can I let the user select it?

You can allow your users to select scripts using the object field:

 var targetScript : MonoScript;

 function OnGUI() {
     targetScript = EditorGUILayout.ObjectField(targetScript, MonoScript, false);
 }


Added: You may find the following of interest: http://docs.unity3d.com/Documentation/ScriptReference/MonoScript.GetClass.html

Comment
Seth-Bergman

People who like this

1 Show 2 · 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 Seth-Bergman · Jan 10, 2013 at 07:29 PM 0
Share

yeah this looks promising, TBC....

avatar image Seth-Bergman · Jan 11, 2013 at 07:35 AM 0
Share

Hmm, well, this is no closer to what I need so far.. No matter how I do it, I am unable to select an existing VARIABLE on a desired script. Using GetClass just returns a generic type too, so I'm no closer to being able to access a var from that script..

for example, if I say:

 targetScript.myTextures[i] = textures[i];

I will get "myTextures is not a member of MonoScript";

if I say:

 var tempScript = target.GetComponent(targetScript.GetClass());

tempScript is a Component, so I still have the same problem:

 tempScript.myTextures ...etc

won't work

avatar image

Answer by Bunny83 · Jan 11, 2013 at 08:44 AM

You have two options:

  • Use a common baseclass or interface that all your classes derive from / implement. So each class has a SetTextures function. Now use GetComponent with the base class or the interface and use the SetTextures function.

  • Use reflection. Unity itself uses a lot reflection internally for such t$$anonymous$$ngs, however if t$$anonymous$$s should be a system that should be used by others as well it's better to keep it more robust. Having the user type in variable names can lead to unexpected t$$anonymous$$ngs. If you use it just for yourself it shouldn't be a problem (as long as you know what you can do and what not ;))

I don't have time for a full example, maybe later.

Comment

People who like this

0 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 Seth-Bergman · Feb 04, 2013 at 07:11 PM 0
Share

-thanks.. Using a base class would work, though I was hoping for something generic (it's not necessary by any means, just for learning purposes at this point). At any rate, since some of my objects have more texture arrays than others, I suppose (using this method) I would give my base class an array of arrays(?) so that I could make that work.. If I ever do something like this again, I could go that route..

-since I am largely interested in concepts which I am not yet familiar with, I appreciate the suggestion of REFLECTION. I am going to play with this a bit now, (my attempt) starting off of this example:

http://msdn.microsoft.com/en-us/library/k3a58006%28v=vs.80%29.aspx

(or actually I think this is the more relevant link [?])

http://msdn.microsoft.com/en-US/library/a4a92379%28v=vs.80%29.aspx

well, I'll read through the the whole section:

http://msdn.microsoft.com/en-us/library/cxz4wk15%28v=vs.80%29.aspx

certainly looks like exactly what I'm after! TBC...

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

10 People are following this question.

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

Related Questions

Is it possible to have editor-only native plugins? 0 Answers

Undo SetSiblingIndex for root objects? 1 Answer

How to get unity3d editor account id or unique id 0 Answers

How to draw a gizmo with a mouse click? 0 Answers

How to play a particle system from the editor without selecting it 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