using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Runtime.InteropServices; //添加如下命名空间 需要是用 DllImport
//引用内存映射文件命名空间
using System.IO.MemoryMappedFiles;
using System.Security.Principal;
using UnityEngine.Networking;
public class NewBehaviourScript : MonoBehaviour
{
[DllImport(@"unitydll", EntryPoint = "testhello")]
public static extern int testhello(int a, int b);
[DllImport(@"unitydll", EntryPoint = "CreateSharedMemory")]
public static extern void CreateSharedMemory();
[DllImport(@"unitydll", EntryPoint = "DestorySharedMemory")]
public static extern void DestorySharedMemory();
[DllImport(@"unitydll", EntryPoint = "SetVCamData")]
//public static extern void SetVCamData(const unsigned char* lpcData, int nSrcWidth, int nSrcHeight);
public static extern void SetVCamData(System.Byte[] lpcData, int nSrcWidth, int nSrcHeight, int len);
[DllImport(@"user32.dll", EntryPoint = "ShowWindow")]
public static extern bool ShowWindow(System.IntPtr hwnd, int nCmdShow);
[DllImport("user32.dll", EntryPoint = "GetForegroundWindow")]
public static extern System.IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError = true)]
private static extern System.IntPtr FindWindow(string lpClassName, string lpWindowName);
//[DllImport(@"unitydll", EntryPoint = "GetVCamData")]
// public static extern BOOL GetVCamData(System.Byte[] lpcData, int& width, int& height);
public Camera mainCam; //待截图的目标摄像机
RenderTexture rt; //声明一个截图时候用的中间变量
Texture2D t2d ;
int num = 0; //截图计数
int sum2 = 0;
int imageWidth = 800;
int imageHeight = 600;
//定义游戏物体
GameObject m_obj;
void Start () {
//找到物体
m_obj = GameObject.Find("3dgift_loli_dance01");
//设置帧率
Application.targetFrameRate=15;
t2d = new Texture2D(800,600,TextureFormat.RGBA32,false);
rt = new RenderTexture(800, 600, 32);
mainCam.targetTexture = rt;
Debug.Log("test **********");
sum2 = testhello(3,4);
Debug.Log("调用dll接口:"+sum2.ToString());
CreateSharedMemory();
//隐藏窗口
ShowWindow(FindWindow(null, "New Unity Project9"), 0);
}
void Update () {
//开始播放动画
if (Input.GetKeyDown(KeyCode.W))
{
m_obj.transform.GetComponent().wrapMode = WrapMode.Loop;
m_obj.transform.GetComponent().Play ("jump");
}
//停止播放动画
if (Input.GetKeyDown(KeyCode.Q))
{
m_obj.transform.GetComponent().wrapMode = WrapMode.Loop;
m_obj.transform.GetComponent().Stop ("jump");
}
//显示、隐藏物体
if (Input.GetKeyDown(KeyCode.Q))
{
//隐藏物体
m_obj.SetActive(false);
//显示物体
//m_obj.SetActive(true);
}
//按下空格键来截图
//if (Input.GetKeyDown(KeyCode.Space))
{
//将目标摄像机的图像显示到一个板子上
//pl.GetComponent().material.mainTexture = rt;
//CreateSharedMemory();
//截图到t2d中
RenderTexture.active = rt;
//int width = rt.width;
//int height = rt.height;
//Debug.Log("当前截图宽为:"+width.ToString());
//Debug.Log("当前截图高为:"+height.ToString());
t2d.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
t2d.Apply();
RenderTexture.active = null;
//将图片保存起来
// byte[] byt = (byte[])t2d.EncodeToPNG().Clone();
byte[] byt = t2d.EncodeToPNG();
int test_length = byt.Length;
//Debug.Log("当前截图长度为:"+test_length.ToString());
// File.WriteAllBytes(Application.dataPath + "//"+ num.ToString() +".png", byt);
//var data = t2d.GetRawTextureData();
SetVCamData(byt, 800, 600, test_length);
// Debug.Log("当前截图序号为:"+num.ToString());
// num++;
}
}
}