Can ecs and the job system be used for keyboard and mouse inputs?,Can ECS and the jobsystem be used for inputs with keyboard and mouse?
Title Pretty Much says all i want to know, any help is welcome :) PS. If its not really worth using ecs or the job system for inputs then that would be nice to know aswell.,Title really says most of what i want to know. any help is appreciated :)
Answer by PincerGame · Nov 18, 2020 at 11:10 AM
Hello @Krashy, yes you can use input system on your DOTS Projects. Check this sample code that Unity Posted on the GitHub.
Another sample of Mouse event like pointing and clicking code, you can try this ECS code.
public class PointAndClick : MonoBehaviour
{
public static readonly RAYCAST_DISTANCE = 1000;
public Camera Cam;
PhysicsWorld physicsWorld => World.DefaultGameObjectInjectionWorld.GetExistingSystem<BuildPhysicsWorld>().PhysicsWorld;
EntityManager entityManager => World.DefaultGameObjectInjectionWorld.EntityManager;
void LateUpdate()
{
if (!Input.GetMouseButtonDown(0) || Cam == null) return;
var screenPointToRay = Cam.ScreenPointToRay(Input.mousePosition);
var rayInput = new RaycastInput
{
Start = screenPointToRay.origin,
End = screenPointToRay.GetPoint(RAYCAST_DISTANCE),
Filter = CollisionFilter.Default
};
if (!physicsWorld.CastRay(rayInput, out RaycastHit hit)) return;
var selectedEntity = physicsWorld.Bodies[hit.RigidBodyIndex].Entity;
var renderMesh = entityManager.GetSharedComponentData<RenderMesh>(selectedEntity);
var mat = new UnityEngine.Material(renderMesh.material);
mat.SetColor("_Color", UnityEngine.Random.ColorHSV());
renderMesh.material = mat;
entityManager.SetSharedComponentData(selectedEntity, renderMesh);
}
}
Your answer
Follow this Question
Related Questions
Renderer.materials not working in void update() 2 Answers
How do i solve this? CS1061 1 Answer
Movement Animation Looping when it Shouldn't 0 Answers
Ambient Emitter inside a BoxCollider - Rotation not recognized 0 Answers
JsonUtility not working 1 Answer