Briefly describe the difference between a property and a field: A field is a member variable, and a property does provide an interface to external access to an internal member variable. In order to avoid the direct access to the members of the class, it is the encapsulation idea in oop that the attribute appears.
usingUnityengine;usingSystem.Collections; Public classDemotest:monobehaviour {Private int_score; Public intScore {Get{return_score;} Set { if(Value > -) {Value= -; } _score=value; } }}
In the example above, _score is a field or a member variable, and score is actually a simplified method of C # syntax that provides a private member _score external access interface, which triggers _score when we change the value of set{by score ...} Code snippet code execution, which completes the scope of the _score, which embodies the benefits of the encapsulation in oop .... Here's a description of the properties and fields, and here's the subject of this article.
In unity you can serialize the field using [Serializefield] to the Inspector panel (as for what is serialized, the reader can self-Baidu), and of course the public access field is implicitly added [Serializefield], So can be directly serialized, private fields can also be manually added [Serializefield] to achieve serialization, such as the above example I simply add [Serializefield] on the _score, you can directly on the Inspector panel on _ Score is assigned a value
usingUnityengine;usingSystem.Collections; Public classDemotest:monobehaviour {[Serializefield]Private int_score; Public intScore {Get{return_score;} Set { if(Value > -) {Value= -; } _score=value; } }}
You can see this in the Inpector panel:
The reason for this is that score, not _score, is something that Unity3d does when it's displayed, without concern. However, if we directly modify the value of score to 120, then we actually directly modify the value of the _score, rather than through the property score to access, so we limit the _score<=100, it does not work, then if there is such a requirement in the project how to deal with it? There are two options to choose from, first to write the editor in the way that:
usingUnityengine;usingSystem.Collections; Public classDemotest:monobehaviour {//[Serializefield] Private int_score; Public intScore {Get{return_score;} Set { if(Value > -) {Value= -; } Print ("Set _score value ="+value); _score=value; } }}
Custom editor class Source (of course, the class must be placed in the editor directory:
usingUnityengine;usingSystem.Collections;usingUnityeditor; [Customeditor (typeof(Demotest))]//to add a custom label to Demotest Public classEditortest:editor { Public Override voidOninspectorgui ()//triggered when demotest displays changes on the Inspector panel{demotest demotest= Target asDemotest;//target is an object reference to the current operation of the Eiditor internal encapsulation intScore = Editorguilayout.intfield ("score", Demotest.score);//serializes an object on the Inspector panel and associates the Demotest.score property if(Demotest.score! = score)//trigger manually if the property is not triggered after the change{Demotest.score=score; } Base. Drawdefaultinspector ();//back to Inspector panel }}
This way, when we modify the Socre, we can see the corresponding output in the console, which solves the problem of the set and get method encapsulation. Of course, this approach is very limited, very inconvenient to use, there is a simple way to useSetProperty标签,当然我个人由于unity版本过低,
SetProperty还不支持,,应该要5.0以后的版本吧,这里附上链接:https://github.com/LMNRY/SetProperty
Unity3d Inspector panel implements Set/get accessors