일단 Github에 프로젝트 백업용으로 Repository를 만들었습니다.
https://github.com/PieceOfPaper/Unity_ECSStudy
GitHub - PieceOfPaper/Unity_ECSStudy: Unity ECS 공부용 프로젝트
Unity ECS 공부용 프로젝트. Contribute to PieceOfPaper/Unity_ECSStudy development by creating an account on GitHub.
github.com
1. 샘플 에셋 추가
https://assetstore.unity.com/packages/3d/characters/robots/space-robot-kyle-4696
Space Robot Kyle | 3D 로봇 | Unity Asset Store
Elevate your workflow with the Space Robot Kyle asset from Unity Technologies. Find this & other 로봇 options on the Unity Asset Store.
assetstore.unity.com
제가 아는 유니티 공식 3D모델링 중에 가장 좋아합니다.
https://assetstore.unity.com/packages/3d/animations/free-32-rpg-animations-215058
FREE - 32 RPG Animations | 3D 애니메이션 | Unity Asset Store
Elevate your workflow with the FREE - 32 RPG Animations asset from Blink. Find this & other 애니메이션 options on the Unity Asset Store.
assetstore.unity.com
애니메이션이 없으면 어색할 것 같아서 같이 추가했습니다.
Animation Controller에 세팅해두었지만 앞으로 쓸 수 있을지 잘 모르겠습니다.
내 캐릭터(오른쪽), 적캐릭터(왼쪽)로 사용할 수 있도록 Material과 Prefab을 설정해 주었습니다.
2. 구조 설계
// 캐릭터 정보 컴포넌트
public struct CharacterComponent : IComponentData
{
public int HP;
public int MaxHP;
}
// 이동능력 컴포넌트
public struct MovableComponent : IComponentData { }
// 발사능력(?) 컴포넌트
public struct ShootableComponent : IComponentData
{
public int BulletCount;
public int BulletCountMax;
public float BulletReloadTime;
public float BulletReloadTimeMax;
public float BulletShotCooltime;
public float BulletLastShotTime;
}
// 전체 시스템을 가지고 있을 컴포넌트
public struct WorldComponent : IComponentData
{
public Entity playerPrefab;
public Entity enemyPrefab;
}
대충 Component와 Authoring, Baker 우선 만들어두었습니다.
아직 구현하지 않고 "이 정도 필요하겠지?"하고 만든 거라 나중에 수정될 여지는 큽니다.
3. Spawn System 구현
Temp/GeneratedCode/Assembly-CSharp/WorldSystem__System_749383923.g.cs(12,18): error CS0539: 'WorldSystem.__OnUpdate_1FA3752E(ref SystemState)' in explicit interface declaration is not found among members of the interface that can be implemented
WorldSystem에서 내 캐릭터와 적 캐릭터를 스폰할 수 있도록 구현하다가 위와 같은 에러가 발생했습니다.
//Error!
void ISystem.OnUpdate(ref SystemState state) { }
//OK!
public void OnUpdate(ref SystemState state) { }
위처럼 Insterface이름을 넣으니 에러가 났습니다. 실제로는 아래처럼 선언해주어야 정상 작동 합니다.
이번에 에러 덕분에 Temp/GeneratedCode/Assembly-CSharp 경로에 코드를 생성해서 사용한다는 것을 알았네요.
public partial struct WorldSystem : ISystem
{
public void OnUpdate(ref SystemState state)
{
foreach (RefRW<WorldComponent> worldComponent in SystemAPI.Query<RefRW<WorldComponent>>())
{
if (worldComponent.ValueRO.isSpawnedEnemy == false)
{
if (worldComponent.ValueRO.enemyPrefab != Entity.Null)
{
Entity newEntity = state.EntityManager.Instantiate(worldComponent.ValueRO.enemyPrefab);
state.EntityManager.SetComponentData(newEntity, new LocalTransform() { Position = new Vector3(1f, 0f, 0f), Scale = 1f, Rotation = Quaternion.Euler(0f, 180f, 0f) });
}
worldComponent.ValueRW.isSpawnedEnemy = true;
}
if (worldComponent.ValueRO.isSpawnedPlayer == false)
{
if (worldComponent.ValueRO.playerPrefab != Entity.Null)
{
Entity newEntity = state.EntityManager.Instantiate(worldComponent.ValueRO.playerPrefab);
state.EntityManager.SetComponentData(newEntity, new LocalTransform() { Position = new Vector3(-1f, 0f, 0f), Scale = 1f, Rotation = Quaternion.Euler(0f, 180f, 0f) });
}
worldComponent.ValueRW.isSpawnedPlayer = true;
}
}
}
}
구현은 이전에 구현했던 것과 비슷하게 구현했습니다.
4. Skinned Mesh Renderer 문제
System 코드를 짜는 것까지는 어렵지 않았는데 모델링이 이상하게 출력하고 있었습니다.
RobotKyle만의 문제인가 싶어서 다른 모델링들도 끌어와봤는데 다들 정상적으로 출력하고 있지 않습니다.
아무리 찾아봐도 해결 방법이 나오지 않아 일단은 애니메이션은 포기하고 Mesh Renderer로 출력해서 System을 구현하기로 했습니다.
일단 이렇게 하니 나오네요.
https://docs.unity3d.com/Packages/com.unity.rendering.hybrid@0.61/manual/index.html
The Entities Graphics Package replaces the Hybrid Renderer Package. | Hybrid Renderer | 0.61.0-preview.7
docs.unity3d.com
아마도 찾아본 내용으로는 Hybrid Renderer 패키지를 추가하고 Shader Graph에서 Linear Blend Skinning 노드를 추가해서 사용하는 것으로 보입니다.
이 부분은 나중에 천천히 다시 봐야 할 것 같네요.
'Study Log' 카테고리의 다른 글
Unity ECS Study Log - 6 (0) | 2023.01.15 |
---|---|
Unity ECS Study Log - 5 (0) | 2023.01.14 |
Unity ECS Study Log - 4 (0) | 2023.01.09 |
Unity ECS Study Log - 2 (0) | 2023.01.01 |
Unity ECS Study Log - 1 (0) | 2023.01.01 |