属性

確認済のバージョン: 4.2

-

難易度: 中級

属性を使うと、作成したメソッドや変数に追加の挙動をアタッチすることができます。ここでは、属性の形式と "Range" と "ExecuteInEditMode" 属性の使い方を学びます。

属性

中級 Scripting

SpinScript

Code snippet

using UnityEngine;
using System.Collections;

public class SpinScript : MonoBehaviour 
{
    [Range(-100, 100)] public int speed = 0;

    void Update () 
    {
        transform.Rotate(new Vector3(0, speed * Time.deltaTime, 0));
    }
}
#pragma strict

@Range(-100, 100)
public var speed : int = 0;

function Update () 
{
    transform.Rotate(new Vector3(0, speed * Time.deltaTime, 0));
}
import UnityEngine
import System.Collections


public class SpinScript(MonoBehaviour):

    [Range((-100), 100)]
    public speed = 0

    
    private def Update():
        transform.Rotate(Vector3(0, (speed * Time.deltaTime), 0))

ColorScript

Code snippet

using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
public class ColorScript : MonoBehaviour 
{
    void Start()
    {
        renderer.sharedMaterial.color = Color.red;
    }
}
#pragma strict

@ExecuteInEditMode
function Start () 
{
    renderer.sharedMaterial.color = Color.red;
}
import UnityEngine
import System.Collections


[ExecuteInEditMode]
public class ColorScript(MonoBehaviour):

    private def Start():
        renderer.sharedMaterial.color = Color.red