C++RAW #include "UnityPrefix.h" #include "Configuration/UnityConfigure.h" #include "Runtime/Scripting/ScriptingManager.h" #include "Runtime/Scripting/ScriptingExportUtility.h" #include "Runtime/Mono/MonoBehaviour.h" #include "Runtime/Video/VideoTexture.h" CSRAW namespace UnityEngine { CONDITIONAL ENABLE_WEBCAM // *undocumented* ENUM WebCamFlags // Camera faces the same direction as screen FrontFacing = 1, END CONDITIONAL ENABLE_WEBCAM // A structure describing the webcam device. STRUCT WebCamDevice // A human-readable name of the device. Varies across different systems. CSRAW public string name { get { return m_Name; } } // True if camera faces the same direction a screen does, false otherwise. CSRAW public bool isFrontFacing { get { return (m_Flags & ((int)WebCamFlags.FrontFacing)) == 1; } } CSRAW internal string m_Name; CSRAW internal int m_Flags; END CONDITIONAL ENABLE_WEBCAM // WebCam Textures are textures onto which the live video input is rendered CLASS WebCamTexture : Texture CUSTOM private static void Internal_CreateWebCamTexture ([Writable]WebCamTexture self, string device, int requestedWidth, int requestedHeight, int maxFramerate) { WebCamTexture* texture = NEW_OBJECT_MAIN_THREAD (WebCamTexture); texture->Reset(); Scripting::ConnectScriptingWrapperToObject (self.GetScriptingObject(), texture); texture->AwakeFromLoad(kInstantiateOrCreateFromCodeAwakeFromLoad); texture->SetRequestedWidth (requestedWidth); texture->SetRequestedHeight (requestedHeight); texture->SetRequestedFPS (maxFramerate); texture->SetDevice (device); } // Create a WebCamTexture CSRAW public WebCamTexture (string deviceName, int requestedWidth, int requestedHeight, int requestedFPS) { Internal_CreateWebCamTexture (this, deviceName, requestedWidth, requestedHeight, requestedFPS); } ///*listonly* CSRAW public WebCamTexture (string deviceName, int requestedWidth, int requestedHeight) { Internal_CreateWebCamTexture (this, deviceName, requestedWidth, requestedHeight, 0); } ///*listonly* CSRAW public WebCamTexture (string deviceName) { Internal_CreateWebCamTexture (this, deviceName, 0, 0, 0); } ///*listonly* CSRAW public WebCamTexture (int requestedWidth, int requestedHeight, int requestedFPS) { Internal_CreateWebCamTexture (this, "", requestedWidth, requestedHeight, requestedFPS); } ///*listonly* CSRAW public WebCamTexture (int requestedWidth, int requestedHeight) { Internal_CreateWebCamTexture (this, "", requestedWidth, requestedHeight, 0); } ///*listonly* CSRAW public WebCamTexture () { Internal_CreateWebCamTexture (this, "", 0, 0, 0); } // Starts the camera AUTO void Play(); // Pauses the camera. AUTO void Pause(); // Stops the camera AUTO void Stop(); // Returns if the camera is currently playing AUTO_PROP bool isPlaying IsPlaying // Set this to specify the name of the device to use. CUSTOM_PROP string deviceName { return scripting_string_new(self->GetDevice ()); } { self->SetDevice (value); } // Set the requested frame rate of the camera device (in frames per second). AUTO_PROP float requestedFPS GetRequestedFPS SetRequestedFPS // Set the requested width of the camera device. AUTO_PROP int requestedWidth GetRequestedWidth SetRequestedWidth // Set the requested height of the camera device. AUTO_PROP int requestedHeight GetRequestedHeight SetRequestedHeight CONDITIONAL UNITY_IPHONE_API CUSTOM_PROP bool isReadable { return self->IsReadable(); } CONDITIONAL UNITY_IPHONE_API CUSTOM void MarkNonReadable() { self->SetReadable(false); } // Return a list of available devices. CUSTOM_PROP static WebCamDevice[] devices { MonoWebCamDevices devs; WebCamTexture::GetDeviceNames(devs, true); ScriptingClassPtr klass = GetScriptingManager().GetCommonClasses().webCamDevice; ScriptingArrayPtr array = CreateScriptingArray(klass, devs.size()); for (MonoWebCamDevices::size_type i = 0; i < devs.size(); ++i) { #if UNITY_WINRT ScriptingObjectPtr dev = CreateScriptingObjectFromNativeStruct(klass, devs[i]); Scripting::SetScriptingArrayElement(array, i, dev); #else Scripting::SetScriptingArrayElement(array, i, devs[i]); #endif } return array; } // Returns pixel color at coordinates (x, y). CUSTOM Color GetPixel (int x, int y) { return self->GetPixel (x, y); } // Get a block of pixel colors. CSRAW public Color[] GetPixels() { return GetPixels( 0, 0, width, height ); } // Get a block of pixel colors. CUSTOM Color[] GetPixels(int x, int y, int blockWidth, int blockHeight) { int res = blockWidth * blockHeight; if (blockWidth != 0 && blockHeight != res / blockWidth) { return SCRIPTING_NULL; } ScriptingArrayPtr colors = CreateScriptingArray(GetScriptingManager().GetCommonClasses().color, res ); self->GetPixels( x, y, blockWidth, blockHeight, &Scripting::GetScriptingArrayElement(colors, 0)); return colors; } // Returns the pixels data in raw format CUSTOM public Color32[] GetPixels32(Color32[] colors = null) { int w = self->GetDataWidth(); int h = self->GetDataHeight(); if (colors != SCRIPTING_NULL) { int size = GetScriptingArraySize(colors); if (size != w * h) { ErrorStringMsg ("Input color array length needs to match width * height, but %d != %d * %d", size, w, h); return SCRIPTING_NULL; } } else colors = CreateScriptingArray(GetScriptingManager().GetCommonClasses().color32, w * h); self->GetPixels(kTexFormatRGBA32, &Scripting::GetScriptingArrayElement(colors, 0), GetScriptingArraySize(colors) * 4); return colors; } // Returns an clockwise angle, which can be used to rotate a polygon so camera contents are shown in correct orientation. CUSTOM_PROP int videoRotationAngle { return self->GetVideoRotationAngle(); } CUSTOM_PROP bool videoVerticallyMirrored { return self->IsVideoVerticallyMirrored(); } // Did the video buffer update this frame? AUTO_PROP bool didUpdateThisFrame DidUpdateThisFrame END CSRAW }