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 generalhak · Jun 25, 2014 at 02:25 PM · editorcustom editorcustom-editor

how to display variable of function in inspector

hi have a script with 2 function like this :

 public class first: MonoBehaviour {
   static public void one(){
   int numb = 10;
     // something
   }
   static public void two(){
     int helth = 20;
     // something
   }
 }

.. and i have another script like this : .. public class secound: MonoBehaviour {

  Update(){
       //first.one(); // normal use .. but...
  }
 
 }

.... i want to set name of function in inspector like "tow" or "one" then that function execute in Update() in secound class ... and i want after i set the name of function... the variables inside that function showing in inspector .. so.. how do i do 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by fafase · Jun 25, 2014 at 02:51 PM

The way to do this would be to have a Dictionary of string and method/delegate. Below is an example you need to make your own, but the idea is there.

 public class First:MonoBehaviour{
    public string method; // Set in inspector asthe name of the method
    Dictionary<string, Action> dict = new Dictionary<string, Action>();
    void Start(){
       dict.Add("one",new Action(One);
       dict.Add("two",new Action(Two);
       if(dict.Contains(method)) // Make sure the string is in the dictionary
           dict[method]();  // call the method
    }
 }

Best would be to use a enum with the same principle:

 public enum MethodName{
    One, Two
 }

 public class First:MonoBehaviour{
    public MethodName method; // Set in inspector as the name of the method
    Dictionary<MethodName, Action> dict = new Dictionary<MethodName, Action>();
    void Start(){
       dict.Add(MethodName.One,new Action(One);
       dict.Add(MethodName.Two,new Action(Two);
       dict[method]();  // call the method
    }
 }

This way, no chance that the string is wrongly written.

Comment
Add comment · Show 3 · 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 generalhak · Jun 25, 2014 at 04:59 PM 0
Share

thanks for the answer .. but this two codes not working... how do i use it ? .... and i want when i set name of function the variables of that function showing in inspector .. i must use classes or functions for that ? ... i want do this image is a inspector: alt text

avatar image fafase · Jun 25, 2014 at 05:10 PM 0
Share

Then you also need an editor script that based on the choice of the enum, it shows the corresponding variable. There are examples on the internet http://answers.unity3d.com/questions/417837/change-inspector-variables-depending-on-enum.html

avatar image generalhak · Jun 26, 2014 at 01:25 PM 0
Share

Dictionary is a good idea but i want to get parameters of a function in another script then i can show in inspector with a editor script... in below example how can i get name of settext function in another script and then get parameters of that function ? ... i want to recognize functions and parameters of each function in another script... how do i do this ?

 using UnityEngine;
 using System.Collections;
  
 public class first: $$anonymous$$onoBehaviour
 {
     public void settext(string astring)
     {
         guiText.text = astring;
     }
 }

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

2 People are following this question.

avatar image avatar image

Related Questions

Custom editor script for folders? 1 Answer

Handles not displaying 1 Answer

Reset or Reinstantiate Custom Editors Target(s) 1 Answer

Prefabs aren't saving with Undo.RecordObject 4 Answers

Expanding a custom property drawer of a list item forces further items in the list to disappear. 2 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