我如何统一调用c#中的函数

时间:2015-03-13 01:42:56

标签: c# unity3d vuforia

我开发了一个增强现实项目。但我有一个问题。有一个视频播放器脚本可以播放视频。我想添加添加按钮。所以,我为此创建了另一个脚本,但我无法在两个脚本之间建立连接。我的意思是,我想在VideoPlayBackBehaviour.cs中添加可以运行播放功能的按钮

  

VideoHelper.cs

/// <summary>
/// Initializes the VideoPlayerHelper object
/// </summary>
public bool Init()
{
    return videoPlayerInit();
}


/// <summary>
/// Deinitializes the VideoPlayerHelper object
/// </summary>
/// <returns></returns>
public bool Deinit()
{
    return videoPlayerDeinit();
}


/// <summary>
/// Loads a local or remote movie file
/// </summary>
public bool Load(string filename, MediaType requestedType, bool playOnTextureImmediately, float seekPosition)
{
    SetFilename(filename);
    return videoPlayerLoad(mFilename, (int) requestedType, playOnTextureImmediately, seekPosition);
}


/// <summary>
/// Unloads the currently loaded movie
/// After this is called a new load() has to be invoked
/// </summary>
public bool Unload()
{
    return videoPlayerUnload();
}


/// <summary>
/// Indicates whether the movie can be played on a texture
/// </summary>
public bool IsPlayableOnTexture()
{
    return videoPlayerIsPlayableOnTexture();
}


/// <summary>
/// Indicates whether the movie can be played fullscreen
/// </summary>
public bool IsPlayableFullscreen()
{
    return videoPlayerIsPlayableFullscreen();
}


/// <summary>
/// Set the native texture object that the video frames will be copied to
/// </summary>
public bool SetVideoTextureID(int textureID)
{
    return videoPlayerSetVideoTextureID(textureID);
}


/// <summary>
/// Return the current status of the movie such as Playing, Paused or Not Ready
/// </summary>
public MediaState GetStatus()
{
    return (MediaState) videoPlayerGetStatus();
}


/// <summary>
/// Returns the width of the video frame
/// </summary>
public int GetVideoWidth()
{
    return videoPlayerGetVideoWidth();
}


/// <summary>
/// Returns the height of the video frame
/// </summary>
public int GetVideoHeight()
{
    return videoPlayerGetVideoHeight();
}


/// <summary>
/// Returns the length of the current movie
/// </summary>
public float GetLength()
{
    return videoPlayerGetLength();
}


/// <summary>
/// Request a movie to be played either full screen or on texture and at a given position
/// </summary>
public bool Play(bool fullScreen, float seekPosition)
{
    // On Android we use Unity's built-in full screen movie player

    // On iOS we overlay a native full screen player as a new subview of the main window
    // (note that the Unity engine is not paused in this case)

    if (fullScreen && (Application.platform == RuntimePlatform.Android))
    {
        if (mFilename == null)
        {
            return false;
        }

        Handheld.PlayFullScreenMovie(mFullScreenFilename, Color.black, FullScreenMovieControlMode.Full, FullScreenMovieScalingMode.AspectFit);
        return true;
    }
    else
    {
        return videoPlayerPlay(fullScreen, seekPosition);
    }
}


/// <summary>
/// Pauses the current movie being played
/// </summary>
public bool Pause()
{
    return videoPlayerPause();
}


/// <summary>
/// Stops the current movie being played
/// </summary>
public bool Stop()
{
    return videoPlayerStop();
}


/// <summary>
/// Tells the VideoPlayerHelper to update the data from the video feed
/// </summary>
public MediaState UpdateVideoData()
{
    return (MediaState) videoPlayerUpdateVideoData();
}


/// <summary>
/// Moves the movie to the requested seek position
/// </summary>
public bool SeekTo(float position)
{
    return videoPlayerSeekTo(position);
}


/// <summary>
/// Gets the current seek position
/// </summary>
public float GetCurrentPosition()
{
    return videoPlayerGetCurrentPosition();
}


/// <summary>
/// Sets the volume of the movie to the desired value
/// </summary>
public bool SetVolume(float value)
{
    return videoPlayerSetVolume(value);
}


/// <summary>
/// Gets the buffering percentage in case the movie is loaded from network
/// Note this is not supported on iOS
/// </summary>
public int GetCurrentBufferingPercentage()
{
    return videoPlayerGetCurrentBufferingPercentage();
}


/// <summary>
/// Allows native player to do appropriate on pause cleanup
/// </summary>
public void OnPause()
{
    videoPlayerOnPause();
}
  

PlayBackBehavior.cs

private VideoPlayerHelper mVideoPlayer = null;
private bool mIsInited = false;
private bool mIsPrepared = false;

private Texture2D mVideoTexture = null;

[SerializeField]
[HideInInspector]
private Texture mKeyframeTexture = null;

private VideoPlayerHelper.MediaType mMediaType =
        VideoPlayerHelper.MediaType.ON_TEXTURE_FULLSCREEN;

private VideoPlayerHelper.MediaState mCurrentState =
        VideoPlayerHelper.MediaState.NOT_READY;

private float mSeekPosition = 0.0f;

private bool isPlayableOnTexture;

private GameObject mIconPlane = null;
private bool mIconPlaneActive = false;

#endregion // PRIVATE_MEMBER_VARIABLES



#region PROPERTIES

/// <summary>
/// Returns the video player
/// </summary>
public VideoPlayerHelper VideoPlayer
{
    get { return mVideoPlayer; }
}

/// <summary>
/// Returns the current playback state
/// </summary>
public VideoPlayerHelper.MediaState CurrentState
{
    get { return mCurrentState; }
}

/// <summary>
/// Type of playback (on-texture only, fullscreen only, or both)
/// </summary>
public VideoPlayerHelper.MediaType MediaType
{
    get { return mMediaType; }
    set { mMediaType = value; }
}

/// <summary>
/// Texture displayed before video playback begins
/// </summary>
public Texture KeyframeTexture
{
    get { return mKeyframeTexture; }
    set { mKeyframeTexture = value; }
}


/// <summary>
/// Returns whether the video should automatically start
/// </summary>
public bool AutoPlay
{
    get { return m_autoPlay; }
}

#endregion // PROPERTIES



#region UNITY_MONOBEHAVIOUR_METHODS

void Start()
{
    // Find the icon plane (child of this object)
    mIconPlane = transform.Find("Icon").gameObject;

    // A filename or url must be set in the inspector
    if (m_path == null || m_path.Length == 0)
    {
        Debug.Log("Please set a video url in the Inspector");
        HandleStateChange(VideoPlayerHelper.MediaState.ERROR);
        mCurrentState = VideoPlayerHelper.MediaState.ERROR;
        this.enabled = false;
    }
    else
    {
        // Set the current state to Not Ready
        HandleStateChange(VideoPlayerHelper.MediaState.NOT_READY);
        mCurrentState = VideoPlayerHelper.MediaState.NOT_READY;
    }
    // Create the video player and set the filename
    mVideoPlayer = new VideoPlayerHelper();
    mVideoPlayer.SetFilename(m_path);

    // Flip the plane as the video texture is mirrored on the horizontal
    transform.localScale = new Vector3(-1 * Mathf.Abs(transform.localScale.x),
            transform.localScale.y, transform.localScale.z);

    // Scale the icon
    ScaleIcon();
}

void OnRenderObject()
{
    if (!mIsInited)
    {
        // Initialize the video player
        if (mVideoPlayer.Init() == false)
        {
            Debug.Log("Could not initialize video player");
            HandleStateChange(VideoPlayerHelper.MediaState.ERROR);
            this.enabled = false;
            return;
        }

        // Initialize the video texture
        InitVideoTexture();

        // Load the video
        if (mVideoPlayer.Load(m_path, mMediaType, false, 0) == false)
        {
            Debug.Log("Could not load video '" + m_path + "' for media type " + mMediaType);
            HandleStateChange(VideoPlayerHelper.MediaState.ERROR);
            this.enabled = false;
            return;
        }

        // Successfully initialized
        mIsInited = true;
    }
    else if (!mIsPrepared)
    {
        // Get the video player status
        VideoPlayerHelper.MediaState state = mVideoPlayer.GetStatus();

        if (state == VideoPlayerHelper.MediaState.ERROR)
        {
            Debug.Log("Could not load video '" + m_path + "' for media type " + mMediaType);
            HandleStateChange(VideoPlayerHelper.MediaState.ERROR);
            this.enabled = false;
        }
        else if (state < VideoPlayerHelper.MediaState.NOT_READY)
        {
            // Video player is ready

            // Can we play this video on a texture?
            isPlayableOnTexture = mVideoPlayer.IsPlayableOnTexture();

            if (isPlayableOnTexture)
            {
                // Pass the video texture id to the video player
                int nativeTextureID = mVideoTexture.GetNativeTextureID();
                mVideoPlayer.SetVideoTextureID(nativeTextureID);

                // Get the video width and height
                int videoWidth = mVideoPlayer.GetVideoWidth();
                int videoHeight = mVideoPlayer.GetVideoHeight();

                if (videoWidth > 0 && videoHeight > 0)
                {
                    // Scale the video plane to match the video aspect ratio
                    float aspect = videoHeight / (float) videoWidth;

                    // Flip the plane as the video texture is mirrored on the horizontal
                    transform.localScale = new Vector3(-0.1f, 0.1f, 0.1f * aspect);
                }

                // Seek ahead if necessary
                if (mSeekPosition > 0)
                {
                    mVideoPlayer.SeekTo(mSeekPosition);
                }
            }
            else
            {
                // Handle the state change
                state = mVideoPlayer.GetStatus();
                HandleStateChange(state);
                mCurrentState = state;
            }

            // Scale the icon
            ScaleIcon();

            // Video is prepared, ready for playback
            mIsPrepared = true;
        }
    }
    else
    {
        if (isPlayableOnTexture)
        {
            // Update the video texture with the latest video frame
            VideoPlayerHelper.MediaState state = mVideoPlayer.UpdateVideoData();

            // Check for playback state change
            if (state != mCurrentState)
            {
                HandleStateChange(state);
                mCurrentState = state;
            }
        }
        else
        {
            // Get the current status
            VideoPlayerHelper.MediaState state = mVideoPlayer.GetStatus();

            // Check for playback state change
            if (state != mCurrentState)
            {
                HandleStateChange(state);
                mCurrentState = state;
            }
        }
    }

    CheckIconPlaneVisibility();
}


void OnApplicationPause(bool pause)
{
    if (!mIsInited)
        return;

    if (pause)
    {
        // Handle pause event natively
        mVideoPlayer.OnPause();

        // Store the playback position for later
        mSeekPosition = mVideoPlayer.GetCurrentPosition();

        // Deinit the video
        mVideoPlayer.Deinit();

        // Reset initialization parameters
        mIsInited = false;
        mIsPrepared = false;

        // Set the current state to Not Ready
        HandleStateChange(VideoPlayerHelper.MediaState.NOT_READY);
        mCurrentState = VideoPlayerHelper.MediaState.NOT_READY;
    }
}


void OnDestroy()
{
    // Deinit the video
    mVideoPlayer.Deinit();
}
  

ButtonScript,我也尝试了这个,但它没有用。

    void OnMouseDown(){

VideoPlayerHelper  otherScript = GetComponent<VideoPlayerHelper>();

        otherScript.Play();
    }

2 个答案:

答案 0 :(得分:1)

两种解决方案。这些将完全有效,但您必须根据解决方案修改您的代码:

  • 将该按钮作为Unity中Video元素的子项。
  • 或者,在调用playBackBehaviour.cs时,请确保包含a 视频元素的全局变量,以便您可以附加 需要通过按钮直接播放按钮的视频 督察小组。

答案 1 :(得分:0)

我真的不知道什么是问题,如果你只想从其他人那里访问一个脚本。

只需使用脚本类型创建一个变量:

MyScript.cs   
then:

private MyScript script;    
script = GetComponent<MyScript>();

现在,在变量脚本中,您有一个MyScript实例和所有方法。只有将所有脚本附加到同一个GameObject时才能使用。 如果您将脚本附加到其他游戏对象,则需要使用:

private MyScript otherScript;
anotherScript = otherGameObject.GetComponent<MyScript>();

其中otherGameObject是对gameObject的引用,您可以在其中找到要查找的脚本。您也可以直接从编辑器中分配,将脚本分配给变量(变量需要公开),只需拖放脚本即可。

你应该尝试更好地解释你的猜测,如果不是必要的话,不要把所有的代码都放好。只有确切的问题。很难检查所有代码。   如果您有更多麻烦,请查看:

http://unity3d.com/learn/tutorials/modules/beginner/scripting/getcomponent