如何从unity中的c#调用函数
我开发了一个增强现实项目。但我有个问题。有一个视频播放器脚本,可以播放视频。我想添加添加按钮。因此,我为此创建了另一个脚本,但我无法在两个脚本之间建立连接。我的意思是,我想添加一个按钮,可以从videoplaybebehaviour.cs运行播放功能 VideoHelper.cs如何从unity中的c#调用函数,c#,unity3d,vuforia,C#,Unity3d,Vuforia,我开发了一个增强现实项目。但我有个问题。有一个视频播放器脚本,可以播放视频。我想添加添加按钮。因此,我为此创建了另一个脚本,但我无法在两个脚本之间建立连接。我的意思是,我想添加一个按钮,可以从videoplaybebehaviour.cs运行播放功能 VideoHelper.cs // ///初始化VideoPlayerHelper对象 /// 公共bool Init() { 返回videoPlayerInit(); } /// ///取消初始化VideoPlayerHelper对象 ///
//
///初始化VideoPlayerHelper对象
///
公共bool Init()
{
返回videoPlayerInit();
}
///
///取消初始化VideoPlayerHelper对象
///
///
公共图书馆
{
返回videoPlayerDeinit();
}
///
///加载本地或远程电影文件
///
公共bool加载(字符串文件名、MediaType请求类型、bool PlayContext立即、浮点seekPosition)
{
SetFilename(文件名);
返回videoPlayerLoad(mFilename,(int)requestedType,PlayContext立即,seekPosition);
}
///
///卸载当前加载的电影
///调用此函数后,必须调用新的load()
///
公共布尔卸载()
{
返回videoPlayerUnload();
}
///
///指示是否可以在纹理上播放电影
///
公共bool可显示上下文()
{
返回VideoPlayerSplayableContext();
}
///
///指示电影是否可以全屏播放
///
公共bool可显示全屏()
{
返回VideoPlayerSplayableFullScreen();
}
///
///设置视频帧将复制到的本地纹理对象
///
公共bool SetVideoTextureID(int-textureID)
{
返回videoPlayerSetVideoTextureID(textureID);
}
///
///返回电影的当前状态,如播放、暂停或未准备就绪
///
公共媒体状态GetStatus()
{
返回(MediaState)VideoPlayetStatus();
}
///
///返回视频帧的宽度
///
public int GetVideoWidth()
{
返回VideoPlayetVideoWidth();
}
///
///返回视频帧的高度
///
公共int GetVideoHeight()
{
返回VideoPlayetVideoHeight();
}
///
///返回当前电影的长度
///
公共浮点GetLength()
{
返回videoPlayerGetLength();
}
///
///请求在给定位置全屏或纹理播放电影
///
公共bool播放(bool全屏、浮动搜索位置)
{
//在Android上,我们使用Unity内置的全屏电影播放器
//在iOS上,我们将本地全屏播放器作为主窗口的新子视图覆盖
//(请注意,在这种情况下,Unity引擎不会暂停)
if(全屏&&(Application.platform==RuntimePlatform.Android))
{
if(mFilename==null)
{
返回false;
}
掌上电脑.PlayFullScreenMovie(mFullScreenFilename、Color.black、FullScreenMovieControlMode.Full、FullScreenMovieScalingMode.AspectFit);
返回true;
}
其他的
{
返回videoPlayerPlay(全屏,查看位置);
}
}
///
///暂停正在播放的当前电影
///
公共广播暂停()
{
返回videoPlayerPause();
}
///
///停止正在播放的当前电影
///
公共车站
{
返回videoPlayerStop();
}
///
///告诉视频播放器帮助者更新视频源中的数据
///
公共媒体状态UpdateVideoData()
{
返回(MediaState)videoPlayerUpdateVideoData();
}
///
///将电影移动到请求的搜索位置
///
公共bool SeekTo(浮动位置)
{
返回视频播放器seekto(位置);
}
///
///获取当前搜索位置
///
公共浮动GetCurrentPosition()
{
返回VideoPlayetCurrentPosition();
}
///
///将电影的音量设置为所需的值
///
公共布尔设置音量(浮点值)
{
返回videoPlayerSetVolume(值);
}
///
///获取从网络加载电影时的缓冲百分比
///注意:这在iOS上不受支持
///
public int GetCurrentBufferingPercentage()
{
返回videoPlayerGetCurrentBufferingPercentage();
}
///
///允许本机播放器在暂停清理时执行适当的操作
///
公共无效暂停()
{
视频播放暂停();
}
playbebehavior.cs
private VideoPlayerHelper mVideoPlayer=null;
private bool Misited=假;
私有bool准备错误=错误;
私有纹理2D mVideoTexture=null;
[序列化字段]
[hideininstecpt]
私有纹理mKeyframeTexture=null;
私人视频播放器Helper.MediaType媒体类型=
VideoPlayerHelper.MediaType.ON_纹理_全屏;
私人视频播放器Helper.MediaState mCurrentState=
VideoPlayerHelper.MediaState.NOT_READY;
专用浮动位置=0.0f;
私人博物馆可供展示;
私有游戏对象mIconPlane=null;
private bool mIconPlaneActive=false;
#endregion//私有成员变量
#区域属性
///
///返回视频播放器
///
公共视频播放器帮助视频播放器
{
获取{return mVideoPlayer;}
}
///
///返回当前播放状态
///
公共视频播放器Helper.MediaState当前状态
{
获取{return mCurrentState;}
}
///
///播放类型(仅在纹理、全屏或两者兼有)
///
公共视频播放器Helper.MediaType MediaType
{
获取{return mmeditype;}
设置{mMediaType=value;}
}
///
///视频播放开始前显示的纹理
///
公共纹理
{
获取{return mKeyframeTexture;}
设置{mKeyframeTexture=value;}
}
///
///返回视频是否应自动启动
///
公共广播自动播放
{
获取{return m_autoPlay;}
}
#endregion//属性
#区域统一法
void Start()
{
//查找图标平面(此对象的子对象)
mIconPlane=transform.Find(“图标”).gameObject;
//必须在检查器中设置文件名或url
if(m_path==null | | m_path.Length==0)
{
Log(“请在Inspector中设置视频url”);
HandleStateChange(VideoPlayerHelper.MediaState.ERROR);
mCurrentState=VideoPlayerHelper.MediaState.ERROR;
this.enabled=false;
}
其他的
{
//放火
/// <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();
}
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();
}
void OnMouseDown(){
VideoPlayerHelper otherScript = GetComponent<VideoPlayerHelper>();
otherScript.Play();
}
MyScript.cs
then:
private MyScript script;
script = GetComponent<MyScript>();
private MyScript otherScript;
anotherScript = otherGameObject.GetComponent<MyScript>();