Cómo crear propiedades

Tutorial

·

intermediate

·

+0XP

·

5 mins

·

(267)

Unity Technologies

Cómo crear propiedades

Cómo se crean propiedades para acceder a las variables miembros (campos) en una clase.

Languages available:

1. Propiedades

C# Properties in Unity! - Intermediate Scripting Tutorial

Player

using UnityEngine;
using System.Collections;

public class Player
{
    //Member variables can be referred to as
    //fields.
    private int experience;

    //Experience is a basic property
    public int Experience
    {
        get
        {
            //Some other code
            return experience;
        }
        set
        {
            //Some other code
            experience = value;
        }
    }

    //Level is a property that converts experience
    //points into the leve of a player automatically
    public int Level
    {
        get
        {
            return experience / 1000;
        }
        set
        {
            experience = value * 1000;
        }
    }

    //This is an example of an auto-implemented
    //property
    public int Health{ get; set;}
}


Game

using UnityEngine;
using System.Collections;

public class Game : MonoBehaviour 
{
    void Start () 
    {
        Player myPlayer = new Player();

        //Properties can be used just like variables
        myPlayer.Experience = 5;
        int x = myPlayer.Experience;
    }
}

Complete this tutorial