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 insominx · Feb 12, 2013 at 12:03 AM · assetreferencedynamicproperty

What's the best way to store a reference to a script asset?

To clarify, at runtime I'm looking to create and add a script to an object based on what type the reference has. This functionality already exists in the form of MonoScript (ScriptReference/MonoScript.html). However MonoScript only works in the editor.

I've considered the following 2 approaches but they seem either inefficient or error prone:

  1. Use string properties and call gameObject.AddComponent(myString)

  2. Store script asset references as a TextAsset and then call gameObject.AddComponent(myTextAsset.name);

Option 2 might be ok but I don't want Unity to load up the whole script file just to get its name.

Is there a better way?

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

2 Replies

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

Answer by PAEvenson · Feb 12, 2013 at 12:21 AM

You can probably just use an object reference or component reference and use AddComponent. http://docs.unity3d.com/Documentation/ScriptReference/Object.html

http://docs.unity3d.com/Documentation/ScriptReference/Component.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 insominx · Feb 12, 2013 at 12:34 AM 0
Share

I can assign script asset references to an Object variable but it doesn't work for Component. Thanks!

avatar image
1

Answer by numberkruncher · Feb 12, 2013 at 12:41 AM

Scripts are compiled into an assembly and so are all available at runtime. I do not know if the representative assets have to be loaded however.

I would just input the name of the script using a regular string variable.

If you are using Unity 4 then you can implement a custom property drawer and attribute to achieve drag & drop in the inspector:

Usage:

 public class SomeBehaviour : MonoBehaviour {
     [ScriptPicker]
     public string myScriptName;

     public void Foo() {
         gameObject.AddComponent(myScriptName);
     }
 }

Attribute Implementation:

 using UnityEngine;

 public class ScriptPickerAttribute : PropertyAttribute {
 }

Drawer Implementation:

 using UnityEngine;
 using UnityEditor;

 [CustomPropertyDrawer(typeof(ScriptPickerAttribute))]
 public class ScriptPickerPropertyDrawer : PropertyDrawer {
     public override void OnGUI(Rect position, SerializedProperty prop, GUIContent) {
         Rect rect;
         
         int controlID = GUIUtility.GetControlID(FocusType.Passive);
         int objectFieldWidth = 45;
         
         // Detect drag & drop
         CheckDragAndDrop(position, prop, controlID);
         
         // Allow manual text entry
         rect = new Rect(
             position.x,
             position.y,
             position.width - objectFieldWidth - 2,
             position.height
         );
         prop.stringValue = EditorGUI.TextField(
             rect, label, prop.stringValue
         );
         
         // Allow drag & drop or selection from dialog
         rect = new Rect(
             position.xMax - objectFieldWidth,
             position.y,
             objectFieldWidth,
             position.height
         );
         MonoScript script = (MonoScript)EditorGUI.ObjectField(
             rect, null, typeof(MonoScript), false
         );
         if (script != null) {
             prop.stringValue = script.name;
         }
     }
     
     private void CheckDragAndDrop(Rect position, SerializedProperty prop, int controlID) {
         Event e = Event.current;
         EventType eventType = e.GetTypeForControl(controlID);
         switch (eventType) {
             case EventType.DragUpdated:
             case EventType.DragPerform:
                 if (position.Contains(e.mousePosition)
                     && DragAndDrop.objectReferences.Length == 1
                     && DragAndDrop.objectReferences[0] is MonoScript)
                 {
                     if (eventType == EventType.DragUpdated) {
                         DragAndDrop.visualMode = DragAndDropVisualMode.Generic;
                         DragAndDrop.activeControlID = controlID;
                     }
                     else {
                         prop.stringValue = DragAndDrop.objectReferences[0].name;
                         DragAndDrop.AcceptDrag();
                         DragAndDrop.visualMode = DragAndDropVisualMode.None;
                     }
                     e.Use();
                 }
                 break;
         }
     }
 }
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

11 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 avatar image

Related Questions

Need help with a raycast 1 Answer

how can I create a variable to refer to any gameObject script with pragma strict 1 Answer

Selling content with this script (Animating Tiled texture) 2 Answers

Unity Keeps Giving Me Errors On My Scripts 1 Answer

(Yet another) Accessing array in another script - ref or copy? 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