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 Fattie · Jun 23, 2012 at 08:44 PM · javascriptinspectorsettersgetterseditor-classes

Getters issue...

in the file teste.js --->

 class happy
 {
 
 private var _x : float = 0.0;
 function get x () : float { return _x; }
 function set x (value : float)
     {
     Debug.Log("hey someone set the x value using an equals sign! whoa!");
     _bar = value;
     }
 
 function Start()
     {
     Debug.Log("getters and setters in Unityscript are easy and happy");
     }
 }


All no problem. But, if you do this

in the file teste.js --->

var x:int;

it makes a variable "x" in the ingenious "Inspector" (Apple-3) panel of the Unity3D Editor. Great.

You can guess what I'm going to ask ... How can I basically do this ...

 functionOrWhatever setOrWhatever x
   {
   Debug.Log("hey, somebody changed x, perhaps in the Inspector!");
   x = x whatever;
   }

in other words, how to do the getters / setters for the "magic'd" variables in the "implicit" classes in Unityscript

{by "implicit" I just mean the feature where if you can just blurt out code using the magic where the filename becomes the class, etc etc -- I don't actually know what the term is, if any, for that sloppy magic in Unity javascript files}

Or, in a word - if for some reason it's not possible to get at those getters/setters, quite simply

how to know if someone has changed x in the Editor Inspector window?

Sorry for yet another ultra-lame Unityscript question.

Comment
Add comment · Show 1
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 Soviut · Jun 23, 2012 at 11:56 PM 1
Share

This is a QA site. Please phrase the title as a question since it makes it far easier for newcomers to understand what you're trying to solve. If you can't phrase it as a simple question, I'd consider posting the issue on the Unity Forums ins$$anonymous$$d.

1 Reply

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

Answer by Loius · Jun 24, 2012 at 03:23 AM

You can only see public vars in the inspector.

But! There's a solution.

Build a custom editor for your class:

"ClassNameEditor.js" - goes in a folder named "Editor"

 @CustomEditor(ClassName)

 class ClassNameEditor extends Editor {
     function OnInspectorGUI() {
         super.OnInspectorGUI();
         var x : int = EditorGUILayout.IntField("Ecks", target.ecks);
         if ( x < 0 ) x = 0;
         target.ecks = x;
     }
 }

"target" always refers to the currently-being-edited-in-the-Inspector ClassName object, so if ClassName is:

 private var ecks : int;

You now have total control over 'ecks' but the user thinks they own it. Also that code just prevents negative numbers being entered.

Comment
Add comment · 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 Fattie · Jun 24, 2012 at 07:25 AM 0
Share

That is so incredible I will have to think about it for awhile... thanks ...

TBC ... in fact, for a moment setting aside the Inspector issue ... in fact, how the hell do you get at the getters / setters, for a variable which is "magic'd" like that? (ie, is just part of the class equal to the filename.js ?)

is there a way ?!

thanks again !

avatar image Loius · Jun 24, 2012 at 06:37 PM 0
Share

You can still use get/sets -

[test.js]

 class Test extends $$anonymous$$onoBehaviour {
     private var _x : int;
     function get x() : int { return _x; }
     function set x(value:int) { _x = $$anonymous$$athf.$$anonymous$$ax(0,value); }
 }

[tested.js]

 @CustomEditor(Test)
 class TestEd extends Editor {
     function OnInspectorGUI() {
         super.OnInspectorGUI();
         target.x = EditorGUILayout.IntField("X",target.x);
     }
 }

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

6 People are following this question.

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

Related Questions

Make a public variable with a private setter appear in inspector 3 Answers

Getter & Setter version Difference 1 Answer

Unity is not updating my script/variables 1 Answer

HLAPI SyncVars property setter function 1 Answer

Update Variables from script to script? 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