unity版本:2022.3.13f1c1
创建3D URP工程并导入Entities.Graphics包。(Unity的ECS框架只支持URP或HDRP)
- 新建空场景《Test01》
- 在《Test01》中创建一个Cube,并设置位置(-1,0,0)
- 添加以下脚本用以测试
public class SimpleMoveUpDownAuthoring : MonoBehaviour
{
public float Range;
public class Baker : Baker<SimpleMoveUpDownAuthoring>
{
public override void Bake(SimpleMoveUpDownAuthoring authoring)
{
var entity = GetEntity(TransformUsageFlags.Dynamic);
AddComponent(entity, new SimpleMoveUpDownCom { Range = authoring.Range, Start = authoring.transform.position });
}
}
}
public struct SimpleMoveUpDownCom : IComponentData
{
public float Range;
public float3 Start;
}
public partial struct SimpleMoveSystem : ISystem
{
public void OnUpdate(ref SystemState state)
{
var t = Time.time;
foreach (var (refMoveUpDown, refLocalToWorld) in SystemAPI.Query<RefRO<SimpleMoveUpDownCom>, RefRW<LocalTransform>>())
{
refLocalToWorld.ValueRW.Position = refMoveUpDown.ValueRO.Start + new float3(0, refMoveUpDown.ValueRO.Range * math.sin(t), 0);
}
}
}
- 在《Test01》中创建子场景(SubScene)《sub01》
- 在sub01内创建一个球(Sphere)并给球添加《SimpleMoveUpDownAuthoring》脚本。
-
在build settting中只添加《Test01》并执行[Build]
测试结果:可以正确显示球并会上下移动。
- 拷贝《Test01》为《Test02》
- 新加以下2个脚本:
public class Test02 : MonoBehaviour
{
private void OnGUI()
{
if (GUILayout.Button("加载Sub03"))
{
LoadSub03();
}
}
void LoadSub03()
{
var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
var builder = new EntityQueryBuilder(Allocator.Temp);
builder.WithAll<SimpleSceneRefCom>();
var query = builder.Build(entityManager);
foreach (var entity in query.ToEntityArray(Allocator.Temp))
{
Debug.Log($"==LoadSub03=={entity}");
var com = entityManager.GetComponentData<SimpleSceneRefCom>(entity);
SceneSystem.LoadSceneAsync(World.DefaultGameObjectInjectionWorld.Unmanaged, com.SceneRef);
}
}
}
public class SimpleRefSceneAuthoring : MonoBehaviour
{
public EntitySceneReference SceneRef;
public class Baker : Baker<SimpleRefSceneAuthoring>
{
public override void Bake(SimpleRefSceneAuthoring authoring)
{
var entity = GetEntity(TransformUsageFlags.None);
AddComponent(entity, new SimpleSceneRefCom { SceneRef = authoring.SceneRef});
}
}
}
public struct SimpleSceneRefCom : IComponentData
{
public EntitySceneReference SceneRef;
}
复制2份《sub01》并分别重命名为《sub02》《sub03》。
-
打开《sub02》,在里面再创建一个球,给新创建的球挂上。《SimpleRefSceneAuthoring》脚本,然后给脚本的SceneRef字段关联到《sub03》。
-
打开《Sub03》并复制2个球,位置稍微错开些。
重新打开《Test02》场景,给cube挂上《Test02》脚本。
-
把《SubScene》脚本的关联场景改为《Sub02》场景。
-
Build Setting中加入《Test02》场景并设置第一启动场景。
执行[Build] 并运行打包出的exe。
-
此时无《sub03》场景中的内容:
-
点击【加载Sub03】按钮后出现《Sub03》中的内容:
- 拷贝《Test02》场景为《Test03》
- 添加以下脚本
/// <summary>
/// 测试加载传统的GameObject场景
/// </summary>
public class Test03 : MonoBehaviour
{
private void OnGUI()
{
if (GUILayout.Button("加载Sub03"))
{
LoadSub03();
}
if (GUILayout.Button("加载 Test03_2"))
{
LoadTest03_2();
}
}
void LoadSub03()
{
var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
var builder = new EntityQueryBuilder(Allocator.Temp);
builder.WithAll<SimpleSceneRefCom>();
var query = builder.Build(entityManager);
foreach (var entity in query.ToEntityArray(Allocator.Temp))
{
Debug.Log($"==LoadSub03=={entity}");
var com = entityManager.GetComponentData<SimpleSceneRefCom>(entity);
SceneSystem.LoadSceneAsync(World.DefaultGameObjectInjectionWorld.Unmanaged, com.SceneRef);
}
}
void LoadTest03_2()
{
var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
var builder = new EntityQueryBuilder(Allocator.Temp);
builder.WithAll<SimpleGameObjectSceneRefCom>();
var query = builder.Build(entityManager);
foreach (var entity in query.ToEntityArray(Allocator.Temp))
{
Debug.Log($"==LoadSub04=={entity}");
var com = entityManager.GetComponentData<SimpleGameObjectSceneRefCom>(entity);
com.GOSceneRef.LoadAsync(new ContentSceneParameters
{
autoIntegrate = true,
loadSceneMode = LoadSceneMode.Additive,
localPhysicsMode = LocalPhysicsMode.None
});
}
}
}
public class SimpleGameObjectSceneAuthoring : MonoBehaviour
{
public WeakObjectSceneReference GOSceneRef;
public class Baker : Baker<SimpleGameObjectSceneAuthoring>
{
public override void Bake(SimpleGameObjectSceneAuthoring authoring)
{
var entity = GetEntity(flags: TransformUsageFlags.None);
AddComponent(entity, new SimpleGameObjectSceneRefCom { GOSceneRef = authoring.GOSceneRef });
}
}
}
public struct SimpleGameObjectSceneRefCom:IComponentData
{
public WeakObjectSceneReference GOSceneRef;
}
复制《Sub02》场景为《Sub04》。
-
新建一个名字为《Test03_2》的场景,里面加入测试UI:
打开《Test03》并把Cube的Test02脚本移除,加上Test03脚本。
把SubScene关联的场景改成《Sub04》。
在《Sub04》内新加一个空GameObject并挂上《SimpleGameObjectSceneAuthoring》脚本,然后关联上《Test03_2》场景。
把《Test03》加入到Build Setting中,并设置成启动场景。
执行【build】。
-
启动exe,当前无Test03_2的内容:
-
点击【加载Test03_2】后显示其内容: