Errors moving it to unity 5
Assets/Scripts/3rd Party/$MeshCombineUtility.cs(93,51): error CS0411: The type arguments for method `System.Linq.Enumerable.Select<TSource,TResult>(this System.Collections.Generic.IEnumerable, System.Func<TSource,int,TResult>)’ cannot be inferred from the usage. Try specifying the type arguments explicitly
Assets/Scripts/3rd Party/$MeshCombineUtility.cs(93,19): error CS1502: The best overloaded method match for `System.Collections.Generic.List<UnityEngine.Vector3>.AddRange(System.Collections.Generic.IEnumerable<UnityEngine.Vector3>)’ has some invalid arguments
Assets/Scripts/3rd Party/$MeshCombineUtility.cs(93,19): error CS1503: Argument #1' cannot convert
object’ expression to type `System.Collections.Generic.IEnumerable<UnityEngine.Vector3>’
code:
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class MeshCombineUtility
{
private readonly List _colors = new List();
private readonly bool _generateStrips;
private readonly List _normals = new List();
private readonly Dictionary<int, List> _strip = new Dictionary<int, List>();
private readonly List _tangents = new List();
private readonly Dictionary<int, List> _triangles = new Dictionary<int, List>();
private readonly List _uv = new List();
private readonly List _uv1 = new List();
private readonly List _vertices = new List();
///
/// Creates a new, empty MeshCombineUtility object for combining meshes.
///
/// true if the meshes you’re going to combine are all triangle-strip based; false if you just want to use triangle lists.
public MeshCombineUtility(bool generateStrips)
{
_generateStrips = generateStrips;
}
///
/// Allocate space for adding a load more vertex data.
///
/// The number of vertices you’re about to add.
public void PrepareForAddingVertices(int numVertices)
{
int shortfall = numVertices - (_vertices.Capacity - _vertices.Count);
_vertices.Capacity += shortfall;
_normals.Capacity += shortfall;
_tangents.Capacity += shortfall;
_uv.Capacity += shortfall;
_uv1.Capacity += shortfall;
_colors.Capacity += shortfall;
}
///
/// Allocate space for adding a load more triangles to the triangle list.
///
/// The index of the submesh that you’re going to add triangles to.
/// The number of triangle indicies (number of triangles * 3) that you want to reserve space for.
public void PrepareForAddingTriangles(int targetSubmeshIndex, int numIndices)
{
if (!_triangles.ContainsKey(targetSubmeshIndex))
_triangles.Add(targetSubmeshIndex, new List());
int shortfall = numIndices - (_triangles[targetSubmeshIndex].Capacity - _triangles[targetSubmeshIndex].Count);
_triangles[targetSubmeshIndex].Capacity += shortfall;
}
///
/// Allocate space for adding a load more triangle strips to the combiner.
///
/// The index of the submesh you’re going to add strips to.
/// A sequence of strip lengths (in number-of-indices). These numbers will be used to automatically calculate how many bridging indices need to be added.
public void PrepareForAddingStrips(int targetSubmeshIndex, IEnumerable stripLengths)
{
if (!_strip.ContainsKey(targetSubmeshIndex))
_strip.Add(targetSubmeshIndex, new List());
int requiredCapacity = _strip[targetSubmeshIndex].Count;
foreach (int srcStripLength in stripLengths)
{
int adjStripLength = srcStripLength;
if (requiredCapacity > 0)
if ((requiredCapacity & 1) == 1)
adjStripLength += 3;
else
adjStripLength += 2;
requiredCapacity += adjStripLength;
}
if (_strip[targetSubmeshIndex].Capacity < requiredCapacity)
_strip[targetSubmeshIndex].Capacity = requiredCapacity;
}
///
/// Add a mesh instance to the combiner.
///
/// The mesh instance to add.
public void AddMeshInstance(MeshInstance instance)
{
int baseVertexIndex = _vertices.Count;
PrepareForAddingVertices(instance.mesh.vertexCount);
_vertices.AddRange(instance.mesh.vertices.Select(instance.transform.MultiplyPoint));
_normals.AddRange(
instance.mesh.normals.Select(n => instance.transform.inverse.transpose.MultiplyVector(n).normalized));
_tangents.AddRange(instance.mesh.tangents.Select(t =>
{
var p = new Vector3(t.x, t.y, t.z);
p =
instance.transform.inverse.transpose.
MultiplyVector(p).normalized;
return new Vector4(p.x, p.y, p.z, t.w);
}));
_uv.AddRange(instance.mesh.uv);
_uv1.AddRange(instance.mesh.uv1);
_colors.AddRange(instance.mesh.colors);
if (_generateStrips)
{
int[ ] inputstrip = instance.mesh.GetTriangles(instance.subMeshIndex);
PrepareForAddingStrips(instance.targetSubMeshIndex, new[ ] {inputstrip.Length});
List outputstrip = _strip[instance.targetSubMeshIndex];
if (outputstrip.Count != 0)
{
if ((outputstrip.Count & 1) == 1)
{
outputstrip.Add(outputstrip[outputstrip.Count - 1]);
outputstrip.Add(inputstrip[0] + baseVertexIndex);
outputstrip.Add(inputstrip[0] + baseVertexIndex);
}
else
{
outputstrip.Add(outputstrip[outputstrip.Count - 1]);
outputstrip.Add(inputstrip[0] + baseVertexIndex);
}
}
outputstrip.AddRange(inputstrip.Select(s => s + baseVertexIndex));
}
else
{
int[ ] inputtriangles = instance.mesh.GetTriangles(instance.subMeshIndex);
PrepareForAddingTriangles(instance.targetSubMeshIndex, inputtriangles.Length);
_triangles[instance.targetSubMeshIndex].AddRange(inputtriangles.Select(t => t + baseVertexIndex));
}
}
///
/// Add multiple mesh instances to the combiner, allocating space for them all up-front.
///
/// The instances to add.
public void AddMeshInstances(IEnumerable instances)
{
instances = instances.Where(instance => instance.mesh);
PrepareForAddingVertices(instances.Sum(instance => instance.mesh.vertexCount));
foreach (var targetSubmesh in instances.GroupBy(instance => instance.targetSubMeshIndex))
{
if (_generateStrips)
{
PrepareForAddingStrips(targetSubmesh.Key,
targetSubmesh.Select(instance => instance.mesh.GetTriangles(instance.subMeshIndex).Length));
}
else
{
PrepareForAddingTriangles(targetSubmesh.Key,
targetSubmesh.Sum(instance => instance.mesh.GetTriangles(instance.subMeshIndex).Length));
}
}
foreach (MeshInstance instance in instances)
AddMeshInstance(instance);
}
///
/// Generate a single mesh from the instances that have been added to the combiner so far.
///
/// A combined mesh.
public Mesh CreateCombinedMesh()
{
var mesh = new Mesh
{
name = “Combined Mesh”,
vertices = _vertices.ToArray(),
normals = _normals.ToArray(),
colors = _colors.ToArray(),
uv = _uv.ToArray(),
uv1 = _uv1.ToArray(),
tangents = _tangents.ToArray(),
subMeshCount = (_generateStrips) ? _strip.Count : _triangles.Count
};
if (_generateStrips)
{
foreach (var targetSubmesh in _strip)
mesh.SetTriangles(targetSubmesh.Value.ToArray(), targetSubmesh.Key);
}
else
{
foreach (var targetSubmesh in _triangles)
mesh.SetTriangles(targetSubmesh.Value.ToArray(), targetSubmesh.Key);
}
return mesh;
}
///
/// Combine the given mesh instances into a single mesh and return it.
///
/// The mesh instances to combine.
/// true to use triangle strips, false to use triangle lists.
/// A combined mesh.
public static Mesh Combine(IEnumerable instances, bool generateStrips)
{
var processor = new MeshCombineUtility(generateStrips);
processor.AddMeshInstances(instances);
return processor.CreateCombinedMesh();
}
#region Nested type: MeshInstance
public class MeshInstance
{
///
/// The source mesh.
///
public Mesh mesh;
///
/// The submesh from the source mesh that you want to add to the combiner.
///
public int subMeshIndex;
///
/// The submesh that you want this instance to be combined into. Group instances that should
/// share a material into the same target submesh index.
///
public int targetSubMeshIndex;
///
/// The instance transform.
///
public Matrix4x4 transform;
}
#endregion
}