C# 他にアタッチされたスクリプトの変数を使う Unity
まず同じゲームオブジェクトに
GameScript.csとHokaScript.csとかをアタッチしておく。
・GameScript.cs
using UnityEngine; using System.Collections; public class GameScript : MonoBehaviour { public int myId = 0; }
→まずこのようにアタッチ
他にアタッチしたスクリプトで以下のようにmyIdが呼べる
・HokaScript.cs
GameScript gs = GetComponent<GameScript>();
Debug.Log(gs.myId); // => 0
先ほどのGameScriptで「public int myId = 0;」ではなく、「private int myId = 0;」のように可視性を
下げてしまうと、
Assets/Scripts/Timer.cs(57,46): error CS0122: `GameScript.myId' is inaccessible due to its protection level
のようにエラーが出るの注意。
C#の「Func」Unity
http://msdn.microsoft.com/ja-jp/library/bb549151.aspxpublic delegate TResult Func<in T, out TResult>( T arg )
・Func
Func<string, string> inCallback;
Funcは必ず返り値を返さなければならない関数。
→「<>の中にある1番目のstring」は引数ではなくて、"返り値の型"
→「<>の中にある次のstring」は"引数"。
→inCallback
・ラムダ式
(myText)=>{ // process return string.Empty; }
→myTextは返り値
↓これもOK
(string myText) => { // process return string.Empty; }
・ラムダ式は空もOK
() => {}
色がなぜ関わらない Cube
CubeにR255の赤を設定したいがこれはなぜかだめusing UnityEngine; using System.Collections; public class CubeColorChanger: MonoBehaviour { GameObject cube; // Use this for initialization void Start () { cube = GameObject.Find("Cube"); } // Update is called once per frame void Update () { /// cube.renderer.material.color = new Vector3(255,0,0); // Vector3とColorはC#ではできないみたい // cube.renderer.material.color = new Color(255,0,0); // だめだった cube.GetComponent<MeshRenderer>().material.color = new Color(255,0,0); // だめだった } }