第一种,用自带的API来做,灵活性一般:Application.CaptureScreenshot ("Screenshot.png");
第二种,读屏幕的图像并保存,需要在协程里面等待一帧,示例如下:
IEnumerator OnScreenCapture ()
{
yield return new WaitForEndOfFrame();//等待这一帧画完了才能截图
try
{
int width = Screen.width;
int height = Screen.height;
Texture2D tex = new Texture2D ( width, height, TextureFormat.RGB24, false);//新建一张图
tex.ReadPixels (new Rect (0, 0, width, height), 0, 0, true);//从屏幕开始读点
byte[] imagebytes = tex.EncodeToJPG ();//用的是JPG(这种比较小)
tex.Compress (false);
tex.Apply();
Texture2D mScreenShotImgae = tex;
File.WriteAllBytes ( @"E:\Screenshot.png", imagebytes);
}
catch (System.Exception e)
{
Debug.Log ("ScreenCaptrueError:" + e);
}
}
如果有路过的大神知道其他的截图方法,请一定要告知我,万分感谢。
你要记住一点,你说的脚本就是一个类,调用脚本中的函数就是调用类的成员方法
首先要得到飞机的游戏物体,
如果你说的飞机是敌人的话说明不止有一个,用下面的方法:
c#:
gameobject feiji:
xueliang _xueliang;
_xueliang = feiji.getcomponent
_xuelaing.kouxue();
这里的调用想必是你的炮弹碰撞到飞机时候触发的吧,这样的话你可以在触发事件里得到碰撞到的飞机物体
如果是玩家,就直接用单例模式吧,代码如下:
class myfeiji : monobehavior{
public static myfeiji instance;
void start()
{ instance = this;}
public void kouxue(float point)
{
// your code.......
}
}
private IEnumerator GetScreenShot(string mfileName)
{
yield return new WaitForEndOfFrame();
var texture = ScreenCapture.CaptureScreenshotAsTexture();
byte[] tx = texture.EncodeToPNG();
try
{
File.WriteAllBytes(mfileName, tx);
}
catch (Exception ex)
{
Debug.LogError(ex.Message);
}
}