Last active
July 16, 2019 09:48
-
-
Save tsubaki/8492094 to your computer and use it in GitHub Desktop.
ネイティブプラグインが作成したテクスチャをUnityで使用するサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System; | |
using System.Runtime.InteropServices; | |
public class LoadTexture : MonoBehaviour { | |
[DllImport ("__Internal")] | |
private static extern IntPtr CreateTexture(string filename, out int w, out int h); | |
Texture2D tex = null; | |
void Start() | |
{ | |
tex = new Texture2D(128,128, TextureFormat.ARGB32, false); | |
renderer.material.mainTexture = tex; | |
} | |
void OnGUI() | |
{ | |
// アップデートする場合 | |
if( GUILayout.Button("Push", GUILayout.Width(300), GUILayout.Height(100))) | |
{ | |
int width, height; | |
IntPtr ptr = CreateTexture(Application.streamingAssetsPath + "/icon.png", out width, out height); | |
tex.UpdateExternalTexture(ptr); | |
} | |
// 新規にテクスチャ作る場合 | |
if( GUILayout.Button("Push", GUILayout.Width(300), GUILayout.Height(100))) | |
{ | |
int width, height; | |
IntPtr ptr = CreateTexture(Application.streamingAssetsPath + "/icon2.png", out width, out height); | |
Texture2D nativeTexture = Texture2D.CreateExternalTexture( width, height, TextureFormat.ARGB32, false,false,ptr); | |
tex.UpdateExternalTexture(nativeTexture.GetNativeTexturePtr()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NativePlugins.mm | |
// Unity-iPhone | |
// | |
// Created by tsubaki_t1 on 2014/01/18. | |
// | |
extern "C" | |
{ | |
void* CreateTexture(const char* fileName, int * w, int* h) | |
{ | |
NSString* imageName = [NSString stringWithUTF8String:fileName]; | |
UIImage* teximage = [[UIImage imageWithContentsOfFile: imageName] retain]; | |
CGImageRef textureImage = teximage.CGImage; | |
unsigned texWidth = CGImageGetWidth(textureImage); | |
unsigned texHeight = CGImageGetHeight(textureImage); | |
*w = (int)texWidth; | |
*h = (int)texHeight; | |
GLubyte *textureData = (GLubyte *)malloc(texWidth * texHeight * 4); | |
CGContextRef textureContext = CGBitmapContextCreate(textureData, texWidth, texHeight, 8, texWidth * 4, | |
CGImageGetColorSpace(textureImage), kCGImageAlphaPremultipliedLast); | |
CGContextDrawImage(textureContext, CGRectMake(0.0, 0.0, (float)texWidth, (float)texHeight), textureImage); | |
CGContextRelease(textureContext); | |
GLuint texid; | |
glGenTextures(1, &texid); | |
glBindTexture(GL_TEXTURE_2D, texid); | |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA,GL_UNSIGNED_BYTE, textureData); | |
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT); | |
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT); | |
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); | |
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); | |
[textureImage release]; | |
free(textureData); | |
return (void*)(intptr_t)texid; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I try your code . It crashed in TextureMetal :: AddCreatedTexture(TextureID , id , bool)
If you know how to fix ? Please email me.My email : [email protected]
Thanks