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;
using Spine.Unity;
using UnityEngine.UI;
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;
//定义游戏物体(spine动画对象)
GameObject m_obj2;
//定义静态图片
GameObject m_obj3;
public string _Path ="Assets/pic/Dwarf.png";
//public string _Path ="E:/work/Unity-3D/pic/Dwarf.png";
public string _texname = "Dwarf.png";
private Texture2D m_Tex;
//定义静态图片
void Start () {
//找到物体
m_obj = GameObject.Find("3dgift_loli_dance01");
//找到spine动画物体
m_obj2 = GameObject.Find("Spine GameObject (game_wx_role1_4)");
//设置帧率
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();
//加载静态图片
m_obj3 = GameObject.CreatePrimitive(PrimitiveType.Plane);
//GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
m_obj3.transform.localRotation = Quaternion.Euler(new Vector3(90, 0,0));//旋转
//m_obj3.transform.localScale = new Vector3 (4.0f, 4.0f, 4.0f);//缩放
//m_obj3.transform.position = new Vector3 (0.0f, 0.0f, 0.0f);//位置
Renderer rend = m_obj3.GetComponent();
LoadFromFile(_Path,_texname);
rend.material.mainTexture = m_Tex;
//静态图片
//隐藏窗口
//ShowWindow(FindWindow(null, "New Unity Project9"), 0);
}
void Awake(){
Debug.Log("Awake");
}
void Update () {
//开始播放动画
if (Input.GetKeyDown(KeyCode.W))
{
m_obj.transform.GetComponent().wrapMode = WrapMode.Loop;
//播放指定动画
m_obj.transform.GetComponent().Play ("jump");
//播放默认动画
//m_obj.transform.GetComponent().Play ();
}
//停止播放动画
if (Input.GetKeyDown(KeyCode.Q))
{
m_obj.transform.GetComponent().wrapMode = WrapMode.Loop;
//停止指定动画播放
m_obj.transform.GetComponent().Stop ("jump");
//停止所有动画播放
//m_obj.transform.GetComponent().Stop ();
}
//显示、隐藏物体
if (Input.GetKeyDown(KeyCode.E))
{
//控制播放spine动画
SkeletonAnimation skeletonAnimation = m_obj2.transform.GetComponent();
Spine.AnimationState spineAnimationState = skeletonAnimation.state;
spineAnimationState.SetAnimation(0, "attack2", true);//第一个参数为优先级,第二个参数是动画的名字,第三个参数 true是循环播放,false是只播放一次。 此方法只适合播放一个动画
//spineAnimationState.addAnimation(0, "walk", true);//播放新动画,可以播放多个
//spineAnimationState.setMix("walk", "attack", 0.2f);//混合动画,衔接动画;B动画 在 A动画 播放多久开始衔接播放
//隐藏物体
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++;
}
}
void LoadFromFile(string path,string _name)
{
m_Tex = new Texture2D(1, 1);
// m_Tex.LoadImage(ReadPNG(path+ _name));
m_Tex.LoadImage(ReadPNG(path));
}
byte[] ReadPNG(string path)
{
FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
fileStream.Seek(0, SeekOrigin.Begin);
byte[] binary = new byte[fileStream.Length]; //创建文件长度的buffer
fileStream.Read(binary, 0, (int)fileStream.Length);
fileStream.Close();
fileStream.Dispose();
fileStream = null;
return binary;
}
}