unity3d c#脚本例子

19 篇文章 0 订阅

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<Animation>().wrapMode = WrapMode.Loop;

m_obj.transform.GetComponent<Animation>().Play ("jump");

}

//停止播放动画

if (Input.GetKeyDown(KeyCode.Q))

{

m_obj.transform.GetComponent<Animation>().wrapMode = WrapMode.Loop;

m_obj.transform.GetComponent<Animation>().Stop ("jump");

}

//显示、隐藏物体

if (Input.GetKeyDown(KeyCode.Q))

{

//隐藏物体

m_obj.SetActive(false);

//显示物体

//m_obj.SetActive(true);

}

        //按下空格键来截图

        //if (Input.GetKeyDown(KeyCode.Space))

        {

            //将目标摄像机的图像显示到一个板子上

            //pl.GetComponent<Renderer>().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<Color32>();

 

SetVCamData(byt, 800, 600, test_length);

           // Debug.Log("当前截图序号为:"+num.ToString());

           // num++;

        }          

    }

 

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值