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 jorrit5477 · Oct 18, 2010 at 02:44 PM · editorinspectoreditor-scriptingcustom-inspector

Script to create instance from in inspector

I have a class, SplineController, which I want to allow to have different types of splines to be used for different purposes (as they have different properties). All my spline classes implement an interface, ISpline.

public class SplineController { public ISpline positionSpline;

 public void Start()
 {
     // Obtain SplineControllerWaypoint objects and add points to the ISpline
     SplineAnimationControllerWaypoint[] waypoints =
         transform.parent.GetComponentsInChildren<SplineAnimationControllerWaypoint>();
     foreach (SplineAnimationControllerWaypoint waypoint in waypoints)
     {
         positionSpline.AddPoint(waypoint.position);
     }

     positionSpline.Build();
 }

}

Now I would like to specify which script to use to create the instance of ISpline from through the inspector. What is the way to select the script and create the ISpline instance from there?

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
0

Answer by Adam Rademacher · Oct 18, 2010 at 03:01 PM

If you have an instance of "SplineController" in the game, it should have a field for your public ISpline. Drag and drop the ISpline you want to reference onto that game object like you would any other component reference.

If you want to do it dynamically (through scripting), you'll have to probably do GameObject.Find or .FindWithTag and GetComponent();

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 jorrit5477 · Oct 19, 2010 at 01:46 PM

In order for that to work ISpline can't be an interface (the ISpline property is not visible), but must at least inherit from UnityEngine.Object. If I do that, I am still not able to drag or select a script onto the property field (there is nothing to select).

I have managed to get it up and running: - Made ISpline and an abstract base class, which inherits from MonoBehaviour - Hide the spline property in the inspector - Users must add ISpline implementations to the same game object the spline controller is added to. - SplineController uses the first ISpline implementation found

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 yoyo · Jan 13, 2011 at 03:19 AM 0
Share

You could avoid the abstract base class by iterating through all the components looking for one whose type implements the ISpline interface -- "if (component.GetType().GetInterface("ISpline") != null)"

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

No one has followed this question yet.

Related Questions

How can i get SerializedProperty from UnityEvent which in List. Sorry for my Eng. 2 Answers

OnInspectorGUI changes reset when played in editor or building 2 Answers

How can I recreate the Array Inspector element for a custom Inspector GUI? 7 Answers

How to Hide/Show List or Array in the inspector based on a variable? 0 Answers

Create inspector drop-down button based on the content of a list in editor mode 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