// スプライトのサイズをセットする. public void setSizeToSprite(Sprite2DControl sprite, Vector2 size) { sprite.internalSetSize(size); Vector3[] positions = this.calcVertexPositions(sprite.getSize().x, sprite.getSize().y, sprite.getDivCount()); this.setVertexPositionsToSprite(sprite, positions); }
// 스프라이트 크기를 설정한다. public void setSizeToSprite(Sprite2DControl sprite, Vector2 size) { sprite.internalSetSize(size); Vector3[] positions = this.calcVertexPositions(sprite.getSize().x, sprite.getSize().y, sprite.getDivCount()); this.setVertexPositionsToSprite(sprite, positions); }
// スプライトをつくる. public Sprite2DControl createSprite(Texture texture, int div_count, bool is_transparent) { GameObject go = new GameObject(); go.name = "sprite-2d/"; if (texture != null) { go.name += texture.name; } go.layer = LayerMask.NameToLayer(LAYER_NAME); go.transform.parent = this.root_sprite.transform; go.transform.localPosition = new Vector3(0.0f, 0.0f, BASE_DEPTH); go.transform.localRotation = Quaternion.identity; go.transform.localScale = Vector3.one; MeshRenderer mesh_render = go.AddComponent <MeshRenderer>(); MeshFilter mesh_filter = go.AddComponent <MeshFilter>(); float w = 32.0f; float h = 32.0f; if (texture != null) { w = texture.width; h = texture.height; } // ---------------------------------------------------------------- // // 位置. Vector3[] positions = this.calcVertexPositions(w, h, div_count); // ---------------------------------------------------------------- // // UV. Vector2[] uvs = new Vector2[div_count * div_count]; float du = 1.0f / ((float)div_count - 1.0f); float dv = 1.0f / ((float)div_count - 1.0f); for (int y = 0; y < div_count; y++) { for (int x = 0; x < div_count; x++) { uvs[y * div_count + x] = new Vector2(0.0f + (float)x * du, 0.0f + (float)y * dv); } } // ---------------------------------------------------------------- // // 頂点カラー. Color[] colors = new Color[div_count * div_count]; for (int y = 0; y < div_count; y++) { for (int x = 0; x < div_count; x++) { colors[y * div_count + x] = Color.white; } } // ---------------------------------------------------------------- // // インデックス. int[] indices = new int[(div_count - 1) * (div_count - 1) * 3 * 2]; int n = 0; for (int y = 0; y < div_count - 1; y++) { for (int x = 0; x < div_count - 1; x++) { indices[n++] = (y + 1) * div_count + (x + 0); indices[n++] = (y + 1) * div_count + (x + 1); indices[n++] = (y + 0) * div_count + (x + 1); indices[n++] = (y + 0) * div_count + (x + 1); indices[n++] = (y + 0) * div_count + (x + 0); indices[n++] = (y + 1) * div_count + (x + 0); } } mesh_filter.mesh.vertices = positions; mesh_filter.mesh.uv = uvs; mesh_filter.mesh.colors = colors; mesh_filter.mesh.triangles = indices; // ---------------------------------------------------------------- // // マテリアル. if (is_transparent) { mesh_render.material = new Material(this.shader_transparent); } else { mesh_render.material = new Material(this.shader_opaque); } mesh_render.material.mainTexture = texture; // Sprite2DControl sprite = go.AddComponent <Sprite2DControl>(); sprite.internalSetSize(new Vector2(w, h)); sprite.internalSetDivCount(div_count); //this.sprite_depth -= 0.01f; return(sprite); }