using System; using System.Runtime.InteropServices; using UnityEngine;
publicclassPluginImport : MonoBehaviour { [DllImport("unity_api", CallingConvention = CallingConvention.Cdecl)] publicstaticexternfloatAdd(float a, float b); // Start is called before the first frame update voidStart() { Debug.Log(Add(1, 2)); }
using System; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.UI; using System.Runtime.InteropServices;
publicclassWebCamera : MonoBehaviour { publicstring cam_name = "FaceTime HD Camera"; publicint cam_width = 1280; publicint cam_heidht = 720; publicint cam_fps = 30; public AspectRatioFitter aspect_fitter; private WebCamTexture web_cam_tex = null; privatebool is_init = false; // Start is called before the first frame update voidStart() { WebCamDevice[] devices = WebCamTexture.devices; for (int i = 0; i < devices.Length; i++) { if (devices[i].name == cam_name) { is_init = true; Debug.Log("Connect camera to " + cam_name); break; } } if (is_init == false) { Debug.Log("Can not find " + cam_name); return; } web_cam_tex = new WebCamTexture(cam_name, cam_width, cam_heidht, cam_fps); if (web_cam_tex == null) { is_init = false; Debug.Log ("Can not new WebCamTexture"); return; } GameObject.FindWithTag("background").GetComponent<RawImage>().texture = web_cam_tex; web_cam_tex.Play(); } voidOnStart (Texture preview) { aspect_fitter.aspectRatio = preview.width / (float)preview.height; }
// Update is called once per frame voidUpdate() { } }
然后将Background节点拖拽到Main Camera节点中,Web Camera (Script)项目下的Aspect_fitter项目上:
using System; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.UI; using System.Runtime.InteropServices;
publicclassPluginImport : MonoBehaviour { [DllImport("unity_api", CallingConvention = CallingConvention.Cdecl)] publicstaticexternboolReadImage(string color_img_file, ref IntPtr data, refint width, refint height, refint size); // Start is called before the first frame update voidStart() { IntPtr data = IntPtr.Zero; int width = 0; int height = 0; int size = 0; bool flag = ReadImage("/Users/luohanjie/Downloads/lenna_color.png", ref data, ref width, ref height, ref size); if (!flag || size <= 0) return; byte[] buffer = newbyte[size]; Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false, false); System.Runtime.InteropServices.Marshal.Copy(data, buffer, 0, size); tex.LoadRawTextureData(buffer); tex.Apply(); RawImage background = GameObject.FindWithTag("background").GetComponent<RawImage>(); background.texture = tex; } }
using System; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.UI; using System.Runtime.InteropServices;
publicclassPluginImport : MonoBehaviour { [DllImport("unity_api", CallingConvention = CallingConvention.Cdecl)] publicstaticexternvoidGetPose([MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] float[] pose); // Start is called before the first frame update voidStart() { float[] obj_pose = newfloat[7]; // x, y, z, qw, qx, qy, qz GetPose(obj_pose); //方法1,根据读取的Pose设置Unity中物体的姿态 GameObject obj = GameObject.FindWithTag("obj_tag_name"); obj.transform.position = new Vector3((float)obj_pose[0], (float)obj_pose[1], (float)obj_pose[2]); obj.transform.rotation = new Quaternion((float)obj_pose[4], (float)obj_pose[5], (float)obj_pose[6], (float)obj_pose[3]); //方法2,根据读取的Pose设置Unity中摄像头的姿态 Camera cam_obj = GameObject.FindWithTag("MainCamera").GetComponent<Camera>(); cam_obj.transform.position = new Vector3((float)obj_pose[0], (float)obj_pose[1], (float)obj_pose[2]); cam_obj.transform.rotation = new Quaternion((float)obj_pose[4], (float)obj_pose[5], (float)obj_pose[6], (float)obj_pose[3]); } }