c# - 当左键单击原始图像时,如何设置做某事?

标签 c# video unity3d movie render-to-texture

我的场景中有多个 rawImage,每个 rawImage 都是视频,即电影纹理。

我想做的是,当我左键单击鼠标按钮时,只有单击的 rawImage 播放视频,但似乎当我左键单击任何地方时,所有视频/movieTexture/rawImage 同时播放。


public class video : MonoBehaviour {

    public MovieTexture movie;
    private AudioSource audio;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

        GetComponent<RawImage> ().texture = movie as MovieTexture;
        audio = GetComponent<AudioSource> ();
        audio.clip = movie.audioClip;
        movie.Play ();
        audio.Play ();

        if (Input.GetMouseButtonDown(0)&& movie.isPlaying)
            movie.Stop ();
        else if (Input.GetMouseButtonDown(0) && !movie.isPlaying)
            movie.Play ();  
    }

}   

最佳答案

您的脚本中缺少的是检查鼠标点击的位置,我们希望排除所有那些在用户按下鼠标时不在鼠标下方的视频。

public class video : MonoBehaviour {

public MovieTexture movie;
private AudioSource audio;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

    GetComponent<RawImage> ().texture = movie as MovieTexture;
    audio = GetComponent<AudioSource> ();
    audio.clip = movie.audioClip;
    movie.Play ();
    audio.Play ();

    if (Input.GetMouseButtonDown(0)){
        RectTransform r = transform.GetComponent<RectTransform>(); //Get's reference to RectTransform
        Vector3 size = new Vector3(r.rect.size.x * r.localScale.x, r.rect.size.y * r.localScale.y, 0); //Size in pixels (scale * default size)
        Vector3 pos = r.localPosition + new Vector3(Screen.width/2f, Screen.height/2f, 0)-size/2; //Position in pixels from the bottom-left corner of Image 
        //(r.localPosition is from the center of screen, that is why I substracted half of
        //the screen and minus half of size of the Image because r.localPosition anchor by default is in the center of Image
        Vector3 mousePos = Input.mousePosition;
        if (mousePos.x > pos.x && mousePos.x < pos.x + size.x && mousePos.y > pos.y && mousePos.y < pos.y + size.y) //This is basic logic of testing if point is inside rect
           if(movie.isPlaying) movie.Stop (); else movie.Play (); 
    }
}
}

关于c# - 当左键单击原始图像时,如何设置做某事?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33669728/

相关文章:

c# - 自定义处理程序不喜欢 Firefox 上的空格

c# - 用于更新的 Mongodb 驱动程序 C# 聚合

video - 无需下载整个视频即可生成屏幕截图

c# - GameObject.FindGameObjectsWithTag() 不工作

c# - 如何通过 TFS API 获取最新的变更集编号

c# - SizeChanged 事件,但仅在全部更改完成后发生

C++ RTSP视频采集实现

image - rawvideo 和 rgb32 值传递给 FFmpeg

unity3d - 在单个网格上渲染多种 Material

c# - 无法转换类型 'UnityEngine.Component'