Exemplo n.º 1
0
        private void ChangeObjectProperty(string textureName, float scale)
        {
            Debug.Log("scale : " + scale);

            if (File.Exists(textureName))
            {
                StartCoroutine(MaxstUtil.loadImageFromFileWithSizeAndTexture(Application.streamingAssetsPath + "/../../" + textureName, (width, height, texture) => {
                    Texture2D localTexture = texture;
                    if (texture)
                    {
                        MeshFilter imagePlaneMeshFilter = gameObject.GetComponent <MeshFilter>();
                        if (imagePlaneMeshFilter.sharedMesh == null)
                        {
                            imagePlaneMeshFilter.sharedMesh      = new Mesh();
                            imagePlaneMeshFilter.sharedMesh.name = "ImagePlane";
                        }

                        float imageW = 1.0f;
                        float imageH = (float)height / (float)width;

                        float vertexWidth  = imageW * 0.5f * scale;
                        float vertexHeight = imageH * 0.5f * scale;
                        imagePlaneMeshFilter.sharedMesh.vertices = new Vector3[]
                        {
                            new Vector3(-vertexWidth, 0.0f, -vertexHeight),
                            new Vector3(-vertexWidth, 0.0f, vertexHeight),
                            new Vector3(vertexWidth, 0.0f, -vertexHeight),
                            new Vector3(vertexWidth, 0.0f, vertexHeight)
                        };

                        targetWidth  = imageW * scale;
                        targetHeight = imageH * scale;

                        imagePlaneMeshFilter.sharedMesh.triangles = new int[] { 0, 1, 2, 2, 1, 3 };
                        imagePlaneMeshFilter.sharedMesh.uv        = new Vector2[]
                        {
                            new Vector2(0, 0),
                            new Vector2(0, 1),
                            new Vector2(1, 0),
                            new Vector2(1, 1),
                        };

                        if (gameObject.GetComponent <MeshRenderer>().sharedMaterial == null)
                        {
                            gameObject.GetComponent <MeshRenderer>().sharedMaterial = new Material(Shader.Find("Unlit/Texture"));
                        }

                        gameObject.GetComponent <MeshRenderer>().sharedMaterial.SetTexture("_MainTex", texture);
                    }
                }));
            }
        }
Exemplo n.º 2
0
        private void SetTargetTexture(string textureName)
        {
            if (File.Exists(textureName))
            {
                StartCoroutine(MaxstUtil.loadImageFromFileWithSizeAndTexture(Application.streamingAssetsPath + "/../../" + textureName, (width, height, texture) => {
                    Texture2D localTexture = texture;
                    if (localTexture)
                    {
                        MeshFilter imagePlaneMeshFilter = gameObject.GetComponent <MeshFilter>();
                        if (imagePlaneMeshFilter.sharedMesh == null)
                        {
                            imagePlaneMeshFilter.sharedMesh      = new Mesh();
                            imagePlaneMeshFilter.sharedMesh.name = "ImagePlane";
                        }

                        float localWidth  = (float)width * 0.5f;
                        float localHeight = (float)height * 0.5f;
                        imagePlaneMeshFilter.sharedMesh.vertices = new Vector3[]
                        {
                            new Vector3(-localWidth, -localHeight, 0.0f),
                            new Vector3(-localWidth, localHeight, 0.0f),
                            new Vector3(localWidth, -localHeight, 0.0f),
                            new Vector3(localWidth, localHeight, 0.0f)
                        };

                        imagePlaneMeshFilter.sharedMesh.triangles = new int[] { 0, 1, 2, 2, 1, 3 };
                        imagePlaneMeshFilter.sharedMesh.uv        = new Vector2[]
                        {
                            new Vector2(0, 0),
                            new Vector2(0, 1),
                            new Vector2(1, 0),
                            new Vector2(1, 1),
                        };

                        if (gameObject.GetComponent <MeshRenderer>().sharedMaterial == null)
                        {
                            gameObject.GetComponent <MeshRenderer>().sharedMaterial = new Material(Shader.Find("Unlit/Texture"));
                        }

                        gameObject.GetComponent <MeshRenderer>().sharedMaterial.SetTexture("_MainTex", texture);
                    }
                }));
            }
        }