Categories: Unity, C #, VS2015
Date Created: 2016-04-16 I. INTRODUCTION
1. Common C # data types
Here's a brief introduction to some of the most commonly used data types when developing games with unity.
(1) Basic type
int, float, string, bool
(2) array
Public string[] Familymembers = new string[]{"Greg", "Kate", "Adam",
"Mia"};
Public gameobject[] Carsinthescene = Gameobject.findgameobjectswithtag ("Car");
(3) List generic lists
Public list<string> list=new list<string> ();
......
List. ADD ("A1");
2. Other data type description
(1) ArrayList
The advantage is that any type of data list can be stored, and the disadvantage is that its efficiency is not as high as the list generic lists.
Public ArrayList arraylist=new ArrayList ();
......
Arraylist.add (10);
Arraylist.add ("abc");
(2) Hashtable
Note: Even if you declare it as public, it is not visible in Unity's viewer. So if it is used, it is generally declared as private for use within the script.
Private Hashtable h=new Hashtable ();
......
H.add ("PlayerName", "AA");
H.add ("Gender", "male");
H.add ("Age", 20);
3. Issues to note when declaring variables
(1) A variable declared as public can be visible in the viewer (Inspector).
(2) A variable declared as public will automatically become a property name in the viewer (Inspector).
(3) Avoid using variable names with ambiguous meanings. For example: Name, speed, score preferably replaced with Playername,carspeed,opponentscore, because the name does not see whose name, the player's? The enemy? If it is more than one player, multiple enemies do not understand whose name it is. Second, beginners easy to make confused problems
1. Where should the script file be added to
You can add a script file to any game object (Gameobject).
Gameobject is the base class for other game objects (Gameobject inherits from the object class). In other words, all other components in unity other than object are inherited from Gameobject.
2. Sequence of script execution
Start (): Called once when the game starts running, and update () is not called at this time.
Update (): Every frame is called once, it is generally used to update the scene parameters.
Lateupdate (): Every frame called update () is called once, and it is generally used to update the camera parameters.
Fixedupdate (): Every fixed physical time interval will be called once, it is generally used to update the performance, energy and other parameters.
Awake (): Called Once before Start () when the game starts to run. It is generally used to initialize parameters.
3, how to know which methods are automatically called
In VS2015 script editing state, right-click to select "Implement monobehabious":
Then tick the window that pops up to add the method (these are the methods that unity calls automatically):
You can also select more than one method at a time, then click "OK" and VS2015 will automatically generate the relevant code.
4, which kind of writing good
Here's an example, although the examples are simple enough to make sense, but they can be very quick to understand, especially when it comes to getting components.
Functional Requirements: Define and modify a value (initialize), after modification, always use this value later.
Stupid writing (very low efficiency):
public void Update ()
{
int age = 25;
}
Reason for "dumb": this makes each frame perform a definition and assign a value once, resulting in low efficiency.
Should be (performed only 1 times):
private int age;
public void Start ()
{
Age = 25;
} third, Monobehaviour class
Monobehaviour is a very important class in unity, it defines the basic scripting behavior, all the script classes need to inherit from it directly or indirectly, the script inevitable event is inherited from Monobehaviour.
In VS2015, right-click the Monobehaviour class and choose Go to definition to see the details of the class.
1. Response function for specific events
In addition to the inevitable events, Monobehaviour defines the response functions for a variety of specific events (such as mouse clicks on models, model collisions, and so on), which begin with the function names on.
The following are common response functions for specific events:
2. accessing components through variables
The purpose of scripting is to define the behavior of the game object (Gameobject), so it is often necessary to access the various components that inherit from the game object (Gameobject) and set the component parameters. For common components built into the system, unity provides a convenient way to access the member variables of the component directly in the script, which are defined in Monobehaviur and inherited by the script.
The following are the common components and their corresponding variables:
Note: If the object does not already have a corresponding component, the variable value for that component is null. Iv. using debug mode to find the right initial value
When run in debug mode (that is, press the play button), the value of the property can be modified directly in the viewer (Inspector), and the values of these properties are automatically restored to their original values after debugging (that is, pressing the play button again).
In this way, it is convenient to find the appropriate initial values for the variables in the script without having to compile and run each time. Locate and then end the debugging, and then modify the initial values of the variables in the script.
"Unity" 6.1 C # Script Basics in Unity