将texture2d从一个对象应用到另一个场景中的另一个对象

时间:2017-03-21 17:29:52

标签: c# android unity3d unity5

我有一个使用设备相机拍照的脚本。它将纹理应用于作为立方体的游戏对象。现在我如何将已应用于此立方体的纹理应用于另一个场景中的另一个立方体。

这是我的相机的下面代码:

using UnityEngine;
using System.Collections;

using CameraShot;

public class CameraShotExample : MonoBehaviour {

string log = "";
void OnEnable()
{

    CameraShotEventListener.onImageSaved += OnImageSaved;
    CameraShotEventListener.onImageLoad += OnImageLoad;
    CameraShotEventListener.onVideoSaved += OnVideoSaved;
    CameraShotEventListener.onError += OnError;
    CameraShotEventListener.onCancel += OnCancel;
}

void OnDisable()
{
    CameraShotEventListener.onImageSaved -= OnImageSaved;
    CameraShotEventListener.onImageLoad -= OnImageLoad;
    CameraShotEventListener.onVideoSaved -= OnVideoSaved;
    CameraShotEventListener.onError -= OnError;
    CameraShotEventListener.onCancel -= OnCancel;
}

void OnImageSaved(string path, ImageOrientation orientation)
{
    log += "\nImage Saved to gallery, path : " + path + ", orientation : " +  orientation;
}

void OnImageLoad(string path,Texture2D tex, ImageOrientation orientation)
{
    GameObject.Find("Cube").GetComponent<Renderer>().material.mainTexture = tex;
    log += "\nImage Saved to gallery, loaded :" + path + ", orientation : " + orientation;

}

void OnVideoSaved(string path)
{
    Debug.Log ("Video Saved at path : "+path);
    log += "\nVideo Saved at path :" + path;
}

void OnError(string errorMsg)
{
    Debug.Log ("Error : "+errorMsg);
    log += "\nError : "+errorMsg;
}

void OnCancel()
{
    Debug.Log ("OnCancel");
    log += "\nOnCancel";
}

void OnGUI()
{
    GUILayout.Label (log);
    float btnWidth = 150;
    float btnHeight = 50;
    float y = Screen.height/2-btnHeight/2 - 50;
    if(GUI.Button(new Rect(Screen.width/2-btnWidth/2,y,btnWidth,btnHeight),"Capture Image"))
    {
        #if UNITY_ANDROID
        AndroidCameraShot.LaunchCameraForImageCapture(false); 
        #elif UNITY_IPHONE
        IOSCameraShot.LaunchCameraForImageCapture(true);// capture and crop
        #endif
    }

    y += 100;
    if(GUI.Button(new Rect(Screen.width/2-btnWidth/2,y,btnWidth,btnHeight),"Get Texture"))
    {
        #if UNITY_ANDROID
        AndroidCameraShot.GetTexture2DFromCamera(false);
        #elif UNITY_IPHONE
        IOSCameraShot.GetTexture2DFromCamera(true);// capture and crop
        #endif
    }

    y += 100;
    if(GUI.Button(new Rect(Screen.width/2-btnWidth/2,y,btnWidth,btnHeight),"Record Video"))
    {
        #if UNITY_ANDROID
        AndroidCameraShot.LaunchCameraForVideoCapture(0);
        //AndroidCameraShot.LaunchCameraForVideoCapture(10);// record for 10 seconds
        #elif UNITY_IPHONE
        IOSCameraShot.LaunchCameraForVideoCapture(0); // record for unlimited time
        //IOSCameraShot.LaunchCameraForVideoCapture(10); // record for 10 sec
        #endif

        GlobalData.userPhoto = Texture2D;

    }
}

}

1 个答案:

答案 0 :(得分:0)

您可以拥有public static Texture2D myTexture;之类的静态字段,当您在OnImageLoad中加载它时,可以将该值添加到静态字段中:

void OnImageLoad(string path,Texture2D tex, ImageOrientation orientation)
{
    GameObject.Find("Cube").GetComponent<Renderer>().material.mainTexture = tex;
    log += "\nImage Saved to gallery, loaded :" + path + ", orientation : " + orientation;

    myTexture = tex;
}

然后,您可以通过执行以下操作从任何脚本轻松访问它:

CameraShotExample.myTexture