forked from UnityCommunity/UnityLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeshSerializable.cs
105 lines (88 loc) · 3.14 KB
/
MeshSerializable.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using UnityEngine;
/// <summary>
/// A class that allows creating and modifying meshes from scripts.
/// </summary>
namespace UnityLibrary
{
[Serializable]
public sealed class MeshSerializable : UnityEngine.Object, ISerializable
{
#region Parameters
public Vector3[] vertices;
public int[] triangles;
public Vector3[] normals;
public Color[] colors;
public Vector4[] tangents;
public Color32[] colors32;
public Vector2[] uv;
public Vector2[] uv2;
public Vector2[] uv3;
public Vector2[] uv4;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="MeshSerializable"/> class.
/// </summary>
public MeshSerializable() : this(new Mesh())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="MeshSerializable"/> class.
/// </summary>
/// <param name="mesh">Mesh.</param>
public MeshSerializable(Mesh mesh)
{
this.vertices = mesh.vertices;
this.triangles = mesh.triangles;
this.normals = mesh.normals;
this.colors = mesh.colors;
this.tangents = mesh.tangents;
this.colors32 = mesh.colors32;
this.uv = mesh.uv;
this.uv2 = mesh.uv2;
this.uv3 = mesh.uv3;
this.uv4 = mesh.uv4;
}
#endregion
#region Operators Overload
public static implicit operator MeshSerializable(Mesh mesh)
{
return new MeshSerializable(mesh);
}
public static implicit operator Mesh(MeshSerializable mesh)
{
Mesh result = new Mesh();
result.vertices = mesh.vertices;
result.triangles = mesh.triangles;
result.normals = mesh.normals;
result.colors = mesh.colors;
result.tangents = mesh.tangents;
result.colors32 = mesh.colors32;
result.uv = mesh.uv;
result.uv2 = mesh.uv2;
result.uv3 = mesh.uv3;
result.uv4 = mesh.uv4;
return result;
}
#endregion
#region ISerializable implementation
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("vertices", this.vertices, typeof(Vector3[]));
info.AddValue("triangles", this.triangles, typeof(int[]));
info.AddValue("normals", this.normals, typeof(Vector3[]));
info.AddValue("colors", this.colors, typeof(Color[]));
info.AddValue("tangents", this.tangents, typeof(Vector4[]));
info.AddValue("colors32", this.colors32, typeof(Color32[]));
info.AddValue("uv", this.uv, typeof(Vector3[]));
info.AddValue("uv2", this.uv2, typeof(Vector3[]));
info.AddValue("uv3", this.uv3, typeof(Vector3[]));
info.AddValue("uv4", this.uv4, typeof(Vector3[]));
}
#endregion
}
}