Skip to content

Commit

Permalink
【ECSLearn】 1 放在subscene里,GameObject不用ConvertToEntity脚本就能直接被调用 2 Share…
Browse files Browse the repository at this point in the history
…d component Data 中如果有引用类型,需要实现Equals接口和GetHashCode接口
  • Loading branch information
hexinping committed Aug 13, 2021
1 parent 143f068 commit 2d05189
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// In a normal game project this ifdef is not required.

#if !UNITY_DISABLE_MANAGED_COMPONENTS
using System;
using Unity.Entities;
using UnityEngine;
using Unity.Transforms;
Expand All @@ -15,6 +16,7 @@ class SimpleMeshRenderingAuthoring : MonoBehaviour, IConvertGameObjectToEntity

public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
{
//放在subscene里,GameObject不用ConvertToEntity脚本就能直接被调用
// Assets in subscenes can either be created during conversion and embedded in the scene
var material = new Material(Shader.Find("Standard"));
material.color = Color;
Expand All @@ -25,6 +27,12 @@ public void Convert(Entity entity, EntityManager dstManager, GameObjectConversio
Mesh = Mesh,
Material = material,
});

dstManager.AddSharedComponentData(entity, new SimpleMeshRendererEx
{
Mesh = Mesh,
Material = material,
});
}
}

Expand All @@ -34,6 +42,29 @@ public class SimpleMeshRenderer : IComponentData
public Material Material;
}

//为什么不用Shared component Data
//Shared component Data 中如果有引用类型,需要实现Equals接口和GetHashCode接口
public struct SimpleMeshRendererEx : ISharedComponentData, IEquatable<SimpleMeshRendererEx>
{
public Mesh Mesh;
public Material Material;
public bool Equals(SimpleMeshRendererEx other)
{
if (Material == null)
return false;
int otherId = other.Material.GetInstanceID();
int id = this.Material.GetInstanceID();
return otherId == id;
}

public override int GetHashCode()
{
if (Material == null)
return 0;
return Material.GetInstanceID();
}
}

[ExecuteAlways]
[AlwaysUpdateSystem]
[UpdateInGroup(typeof(PresentationSystemGroup))]
Expand Down

This file was deleted.

Loading

0 comments on commit 2d05189

Please sign in to comment.