static GameObject CreateSprite(Sprite sprite) { MeshData meshData = MeshUtility.CreateMeshFromSprite(sprite); GameObject obj = new GameObject("Sprite"); obj.AddComponent <MeshRenderer>().material = new Material(instance.cubeMaterial); obj.GetComponent <MeshRenderer>().material.mainTexture = sprite.texture; // instance.cubeMaterial; MeshFilter filter = obj.AddComponent <MeshFilter>(); filter.mesh.Clear(); filter.mesh.vertices = meshData.vertices.ToArray(); filter.mesh.triangles = meshData.triangles.ToArray(); filter.mesh.uv = meshData.uv.ToArray(); filter.mesh.RecalculateNormals(); Vector3 size = obj.AddComponent <BoxCollider>().size; size.z *= 2f; obj.GetComponent <BoxCollider>().size = size; return(obj); }
GameObject CreateSprite(Sprite sprite) { MeshData meshData = MeshUtility.CreateMeshFromSprite(sprite); GameObject obj = new GameObject("PreviewSprite"); MeshRenderer meshRenderer = obj.AddComponent <MeshRenderer>(); //if(Application.isPlaying) { // meshRenderer.material = new Material(previewMaterial); // meshRenderer.material.mainTexture = sprite.texture; //} //else { meshRenderer.sharedMaterial = previewMaterial; meshRenderer.sharedMaterial.mainTexture = sprite.texture; obj.hideFlags = HideFlags.HideAndDontSave; //} MeshFilter filter = obj.AddComponent <MeshFilter>(); //if(Application.isPlaying) { // filter.mesh = new Mesh(); // filter.mesh.Clear(); // filter.mesh.vertices = meshData.vertices.ToArray(); // filter.mesh.triangles = meshData.triangles.ToArray(); // filter.mesh.uv = meshData.uv.ToArray(); // filter.mesh.RecalculateNormals(); //} else { filter.sharedMesh = new Mesh(); filter.sharedMesh.Clear(); filter.sharedMesh.vertices = meshData.vertices.ToArray(); filter.sharedMesh.triangles = meshData.triangles.ToArray(); filter.sharedMesh.uv = meshData.uv.ToArray(); filter.sharedMesh.RecalculateNormals(); //} Vector3 size = obj.AddComponent <BoxCollider>().size; size.z *= 2f; obj.GetComponent <BoxCollider>().size = size; return(obj); }