ページ 11

C#のunityについての質問なのですが

Posted: 2014年5月26日(月) 10:18
by doss
相談したいのはunity環境の画像切り替えなのですが、Texture内の画像切り替えはどのようにしたらよろしいのでしょうか。

コード:

using UnityEngine;
using System.Collections;
using UnityEditor;

public class Animal : MonoBehaviour {
	private GameObject animal;
	private bool state;
        private Texture temp;
	// Use this for initialization
	void Start () {
		animal = GameObject.Find("animal");
		this.state = false;
                this.tmp = renderer.material.mainTexture;
	}

	public void ChangeState(bool state){
		this.state = state;
	}

	void OnGUI(){
		if( this.animal.transform.position.y <= 0 ){
			if( GUI.Button(new Rect (50, 50, 100, 100), "Get") )
				Application.LoadLevel("test2");
                if (renderer.material.mainTexture == temp) {
			renderer.material.mainTexture = Instantiate(Resources.Load("画像"));
		} 
		}
	}
	
	// Update is called once per frame
	void Update () {
		if( Input.GetKey( "left" ) == true )
			animal.transform.Translate((float)0.005,(float)0.005,(float)0.0);
		if( this.state == true )
			if( this.animal.transform.position.y >= 0 ){
				this.animal.transform.Translate(0f,-0.005f,0.0f);
				
			}
	}
}
(

コード:

のC#がわからないのでうまくできてなかったら申し訳ないです)
やりたい操作はResourcesフォルダ内の画像をTextureに変換したいというものです。
説明がへたくそですいません。
よろしくお願いします

Re: C#のunityについての質問なのですが

Posted: 2014年5月27日(火) 13:05
by せんちゃ
InstantiateはまずGameObjectを生成する関数なのでうまくいかないのでないかと思われます。
マテリアルのテクスチャを切り替えるだけであればResources.Load( Path ) as TextureでmainTextureにセットすればいけると思います。
ただ、mainTextureを直接変えて画像切り替えを行うのもいかなるものかと思うので4.3以降であればSprite機能があるのでそちらを使って切り替えたほうがパフォーマンス的に良いかもしれません

unity4-3-で追加された-sprite-機能を使ってみよう-その1

Re: C#のunityについての質問なのですが

Posted: 2014年5月29日(木) 04:46
by doss
せんちゃさん、素晴らしいご指摘ありがとうございました。
おかげで画像の切り替えがうまい具合にできました。
まだまだC#についての見聞が狭かったようです。