热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

unity3dc#脚本例子

usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingSystem.IO

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++;

        }          

    }

 

}

 


推荐阅读
  • 本文介绍了如何在C#中启动一个应用程序,并通过枚举窗口来获取其主窗口句柄。当使用Process类启动程序时,我们通常只能获得进程的句柄,而主窗口句柄可能为0。因此,我们需要使用API函数和回调机制来准确获取主窗口句柄。 ... [详细]
  • 本文探讨了如何在给定整数N的情况下,找到两个不同的整数a和b,使得它们的和最大,并且满足特定的数学条件。 ... [详细]
  • 本文介绍如何使用阿里云的fastjson库解析包含时间戳、IP地址和参数等信息的JSON格式文本,并进行数据处理和保存。 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 本文基于刘洪波老师的《英文词根词缀精讲》,深入探讨了多个重要词根词缀的起源及其相关词汇,帮助读者更好地理解和记忆英语单词。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • ImmutableX Poised to Pioneer Web3 Gaming Revolution
    ImmutableX is set to spearhead the evolution of Web3 gaming, with its innovative technologies and strategic partnerships driving significant advancements in the industry. ... [详细]
author-avatar
木目心磊_559
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有