public static void InitializeWithScene() { var settingsGO = GameObject.Find("Settings"); if (settingsGO == null) { SceneManager.sceneLoaded += OnSceneLoaded; return; } Settings = settingsGO?.GetComponent <TwoStickSettings>(); if (!Settings) { return; } PlayerLook = GetLookFromPrototype("PlayerRenderPrototype"); PlayerShotLook = GetLookFromPrototype("PlayerShotRenderPrototype"); EnemyShotLook = GetLookFromPrototype("EnemyShotRenderPrototype"); EnemyLook = GetLookFromPrototype("EnemyRenderPrototype"); EnemySpawnSystem.SetupComponentData(World.Active.GetOrCreateManager <EntityManager>()); World.Active.GetOrCreateManager <UpdatePlayerHUD>().SetupGameObjects(); var sceneSwitcher = GameObject.Find("SceneSwitcher"); if (sceneSwitcher != null) { NewGame(); } }
private void _AddGravity(int i) { // Entity entity = blockData.a_entities [i].entity ; Entity entity = data.a_entities [i]; // RigidTransform rt = new RigidTransform () ; // rt = RigidTransform.scale ( 5 ) ; // RigidTransform.scale = new System.Func<float, RigidTransform> (5) ; float4x4 f4x4 = float4x4.identity; // set default position/rotation/scale matrix // commandsBuffer.AddComponent ( entity, new TransformMatrix { Value = f4x4 } ) ; //commandsBuffer.AddComponent ( entity, new RigidTransform { } ) ; commandsBuffer.AddComponent(entity, new Position { Value = data.a_addGravityTags [i].f3_position }); // tags commandsBuffer.AddComponent(entity, new GravitySourceTag { }); // commandsBuffer.AddComponent ( entity, new AllowRayCastingTag { } ) ; // renderer MeshInstanceRenderer renderer = Bootstrap.gravitySourceRenderer; // renderer.material.SetColor ( "_Color", Color.blue ) ; commandsBuffer.AddSharedComponent(entity, renderer); //commandsBuffer.SetComponent <> () ; commandsBuffer.RemoveComponent <AddGravitySourceTag> (entity); // block added. Remove tag }
public static void Start() { //获取Cube GameObjectEntity cubeEntity = GameObject.Find("Cube").AddComponent <GameObjectEntity>(); //添加Velocity组件 entityManager.AddComponentData(cubeEntity.Entity, new VelocityComponent { moveDir = new Unity.Mathematics.float3(0, 1, 0) }); //把GameObect.Find放在这里因为场景加载完成前无法获取游戏物体。 GameObject playerGo = GameObject.Find("Player"); //下面的类型是一个Struct, 需要引入Unity.Rendering命名空间 MeshInstanceRenderer playerRenderer = playerGo.GetComponent <MeshInstanceRendererComponent>().Value; Object.Destroy(playerGo); //获取到渲染数据后可以销毁空物体 Entity player = entityManager.CreateEntity(playerArchetype); // Position //添加PlayerComponent组件 entityManager.AddComponentData(player, new PlayerComponent()); // PlayerComponent entityManager.AddComponentData(player, new VelocityComponent()); // VelocityComponent entityManager.AddComponentData(player, new InputComponent()); // InputComponent // 向实体添加共享的数据 entityManager.AddSharedComponentData(player, playerRenderer); // MeshInstanceRenderer //修改实体的Position组件 entityManager.SetComponentData(player, new Position { Value = new Unity.Mathematics.float3(0, 0.5f, 0) }); }
private static void SetIngridientData() { IngredientDefaultLook = GetLookFromPrototype(nameof(IngredientDefaultLook)); //Default for testing IngredientsData = new IngredientData[] { new IngredientData() { IngredientName = "Hot Dog", IngredientLook = GetLookFromPrototype("IngredientHotdogLook"), IngredientCost = 1.99f }, new IngredientData() { IngredientName = "Onion", IngredientLook = GetLookFromPrototype("IngredientOnionLook"), IngredientCost = 1.99f }, new IngredientData() { IngredientName = "Chicken", IngredientLook = GetLookFromPrototype("IngredientChickenLook"), IngredientCost = 1.99f }, new IngredientData() { IngredientName = "Cheese", IngredientLook = GetLookFromPrototype("IngredientCheeseLook"), IngredientCost = 1.99f }, new IngredientData() { IngredientName = "Sausage", IngredientLook = GetLookFromPrototype("IngredientSausageLook"), IngredientCost = 1.99f }, new IngredientData() { IngredientName = "Mushroom", IngredientLook = GetLookFromPrototype("IngredientMushroomLook"), IngredientCost = 1.99f } }; }
void UnityECS() { var manager = World.Active.GetOrCreateManager <EntityManager>(); var archetype = manager.CreateArchetype(typeof(Position), typeof(Rotation), typeof(Scale), typeof(MeshInstanceRenderer)); _unityECSGroup = manager.CreateComponentGroup(archetype.ComponentTypes); var renderer = new MeshInstanceRenderer { castShadows = ShadowCastingMode.On, receiveShadows = true, mesh = mesh, material = material }; for (int i = 0; i < boidCount; ++i) { var entity = manager.CreateEntity(archetype); manager.SetComponentData(entity, new Scale { Value = new float3(boidScale.x, boidScale.y, boidScale.z) }); manager.SetSharedComponentData(entity, renderer); } }
// ---------------------------------------------------- #region // Private Functions /// <summary> /// 渡した表示データからMeshInstanceRendererの生成 /// </summary> /// <param name="data">表示データ</param> /// <returns>生成したMeshInstanceRenderer</returns> MeshInstanceRenderer CreateMeshInstanceRenderer(MeshInstanceRendererData data) { // Sprite to Mesh var mesh = new Mesh(); // Unity.Mathematicsが行優先な都合上、Z-Upみたいな挙動になるので都合を合わせるためにMeshの頂点レベルで回しておく var vertices = Array.ConvertAll(data.Sprite.vertices, _ => (Vector3)_).ToList(); Matrix4x4 mat = Matrix4x4.TRS( new Vector3(0f, 0f, 0f), Quaternion.Euler(90f, 0f, 0f), Vector3.one * this._gameSettings.ObjectScale); for (int i = 0; i < vertices.Count; ++i) { vertices[i] = mat.MultiplyPoint3x4(vertices[i]); } mesh.SetVertices(vertices); mesh.SetUVs(0, data.Sprite.uv.ToList()); mesh.SetTriangles(Array.ConvertAll(data.Sprite.triangles, _ => (int)_), 0); var matInst = new Material(data.Material); // 渡すマテリアルはGPU Instancingに対応させる必要がある var meshInstanceRenderer = new MeshInstanceRenderer(); meshInstanceRenderer.mesh = mesh; meshInstanceRenderer.material = matInst; return(meshInstanceRenderer); }
void StartECSMethod() { var entityMgr = World.Active.GetOrCreateManager <EntityManager>(); var entities = new NativeArray <Entity>(spawnCount, Allocator.Temp); //Two ways of creating entities var archeType = entityMgr.CreateArchetype(typeof(GravityComponentData), typeof(Position), typeof(MeshInstanceRenderer)); entityMgr.CreateEntity(archeType, entities); var meshRenderer = new MeshInstanceRenderer() { mesh = mesh, material = mat, }; //Add Components for (int i = 0; i < entities.Length; ++i) { Vector3 pos = UnityEngine.Random.insideUnitSphere * 40; pos.y = GravitySystem.topY; var entity = entities[i]; entityMgr.SetComponentData(entity, new Position { Value = pos }); entityMgr.SetComponentData(entity, new GravityComponentData { mass = Random.Range(0.5f, 3f), delay = 0.02f * i }); entityMgr.SetSharedComponentData(entity, meshRenderer); } entities.Dispose(); }
protected void Initialize() { EntityManager entityManager = World.Active.GetOrCreateManager <EntityManager>(); gridLook = Bootstrap.GetLookFromPrototype("GridRenderPrototype"); gridArchetype = entityManager.CreateArchetype(typeof(CustomGrid), typeof(Position), typeof(Scale)); Entity grid = entityManager.CreateEntity(gridArchetype); gridLook.mesh = GenerateGridMesh(10, 10); entityManager.AddSharedComponentData(grid, gridLook); entityManager.SetComponentData(grid, new Position { Value = new float3(0.0f, 0.0f, 0.0f) }); entityManager.SetComponentData(grid, new Scale { Value = new float3(1.0f, 1.0f, 1.0f) }); entityManager.SetComponentData(grid, new CustomGrid { xSize = 10, ySize = 10 }); float3[] origins = new float3[gridLook.mesh.vertices.Length]; for (int i = 0; i < origins.Length; i++) { Entity sphereRequest = entityManager.CreateEntity(typeof(SphereRequest)); entityManager.SetComponentData(sphereRequest, new SphereRequest { origin = new float3(gridLook.mesh.vertices[i].x, gridLook.mesh.vertices[i].y, gridLook.mesh.vertices[i].z) }); } }
public static void Init() { var entityManager = World.Active.GetOrCreateManager <EntityManager>(); var firstEntityArchetype = entityManager.CreateArchetype(typeof(Cube), typeof(TransformMatrix), typeof(Position), typeof(Rotation), typeof(FirstSteps), typeof(Player), typeof(PlayerInput)); var turningCubesEntityArchetype = entityManager.CreateArchetype(typeof(Ball), typeof(TransformMatrix), typeof(Position), typeof(Rotation), typeof(MustRotate)); cubeLook = GetLookFromPrototype("cubeLook"); playerLook = GetLookFromPrototype("playerLook"); var player = entityManager.CreateEntity(firstEntityArchetype); entityManager.AddSharedComponentData(player, playerLook); for (int i = 0; i < 10; i++) { var ball = entityManager.CreateEntity(turningCubesEntityArchetype); entityManager.AddSharedComponentData(ball, cubeLook); } World.Active.GetOrCreateManager <EraseBallsSystem>().SetUI(); World.Active.GetOrCreateManager <FollowCameraSystem>().FindCameraTransform(); }
public static MeshInstanceRenderer GetLookFromPrefab(GameObject prefab) { MeshInstanceRenderer result = prefab.GetComponent <MeshInstanceRendererComponent>().Value; prefab.SetActive(false); return(result); }
private Entity CreatePlayer() { this._entityManager = World.Active.GetExistingManager <EntityManager>(); string entityName = "player"; var entity = this._entityManager.CreateEntity(ArchetypeFactory.Instance.getArchetypeByName(entityName)); MeshInstanceRenderer renderer = EntityLookFactory.Instance.getLook(entityName); this._entityManager.SetComponentData(entity, getColliderInfo(renderer)); this._entityManager.SetSharedComponentData(entity, renderer); this._entityManager.SetComponentData(entity, new Jump { Value = this._constants.jumpVelocityY }); this._entityManager.SetComponentData(entity, new PlayerInput { Intensity = this._constants.jumpVelocityX }); return(entity); }
void Start() { Cache(); render = new MeshInstanceRenderer { castShadows = ShadowCastingMode.On, receiveShadows = true, material = new Material(material) { enableInstancing = true, }, mesh = mesh, subMesh = 0, }; PlayerLoopManager.RegisterDomainUnload(DestroyAll, 10000); var world = World.Active = new World("x"); World.Active.CreateManager(typeof(EndFrameTransformSystem)); World.Active.CreateManager(typeof(CountUpSystem), GameObject.Find("Count").GetComponent <TMPro.TMP_Text>()); World.Active.CreateManager <MeshInstanceRendererSystem>().ActiveCamera = GetComponent <Camera>(); ScriptBehaviourUpdateOrder.UpdatePlayerLoop(world); manager = world.GetExistingManager <EntityManager>(); archetype = manager.CreateArchetype(ComponentType.Create <Position>(), ComponentType.Create <MeshInstanceRenderer>(), ComponentType.Create <Static>()); // for (int i = 0; i < 1000000; i++) for (int i = 0; i < 100000; i++) { CreateCube(); } }
void OnPostprocessModel(GameObject go) { Renderer[] renderers = go.GetComponentsInChildren <Renderer>(); if (renderers != null) { foreach (Renderer renderer in renderers) { MeshInstanceRendererComponent r = renderer.gameObject.AddComponent <MeshInstanceRendererComponent>(); MeshFilter mf = r.GetComponent <MeshFilter>(); if (mf == null) { continue; } MeshInstanceRenderer meshInstance = new MeshInstanceRenderer { mesh = mf.sharedMesh, subMesh = 0, receiveShadows = true, castShadows = UnityEngine.Rendering.ShadowCastingMode.On, material = renderer.sharedMaterial }; r.Value = meshInstance; } } }
protected override void OnCreateManager() { if (!Bootstrap.IsValid) { return; } archetype = EntityManager.CreateArchetype( typeof(Position), typeof(Rotation), typeof(Scale), typeof(Velocity), typeof(Acceleration), typeof(NeighborsEntityBuffer), typeof(MeshInstanceRenderer)); group = GetComponentGroup(archetype.ComponentTypes); random = new Unity.Mathematics.Random(853); renderer = new MeshInstanceRenderer { castShadows = ShadowCastingMode.On, receiveShadows = true, mesh = Bootstrap.Boid.mesh, material = Bootstrap.Boid.material, }; }
public static Entity CreatePlayer(EntityManager entityManager, MeshInstanceRenderer renderer) { var player = entityManager.CreateEntity(sPlayerArchetype); entityManager.SetSharedComponentData(player, renderer); return(player); }
protected override void OnStartRunning() { _prefabManager = GameObject.FindObjectOfType <PrefabManager>(); // If we run through the scene switcher we need to wait for OnStartRunning to trigger before we can setup the onetime stuff // since the objects we expect won't be available otherwise if (_shipRenderer != null) { return; } var prefabRenderer = _prefabManager.ShipPrefab.GetComponent <MeshInstanceRendererComponent>().Value; var planetSpawner = GameObject.FindObjectOfType <PlanetSpawner>(); _shipRenderer = new MeshInstanceRenderer[planetSpawner._teamMaterials.Length]; for (var i = 0; i < _shipRenderer.Length; ++i) { _shipRenderer[i] = prefabRenderer; _shipRenderer[i].material = new Material(prefabRenderer.material) { color = planetSpawner._teamMaterials[i].color }; _shipRenderer[i].material.SetColor("_EmissionColor", planetSpawner._teamMaterials[i].color); } base.OnStartRunning(); }
protected override void OnStartRunning() { AgentRenderer = BioCrowdsBootStrap.GetLookFromPrototype("AgentRenderer"); UpdateInjectedComponentGroups(); lastAgentId = 0; //If bioclouds isn't enabled we must use other job to spawn the agents //Here we get the necessary data from the experiment file if (!Settings.experiment.BioCloudsEnabled) { var exp = Settings.experiment.SpawnAreas; parBuffer = new NativeList <Parameters>(exp.Length, Allocator.Persistent); for (int i = 0; i < exp.Length; i++) { Parameters par = new Parameters { cloud = i, goal = exp[i].goal, maxSpeed = exp[i].maxSpeed, qtdAgents = exp[i].qtd, spawnOrigin = exp[i].min, spawnDimensions = new float2(exp[i].max.x, exp[i].max.z) }; parBuffer.Add(par); } AgentAtCellQuantity = new NativeArray <int>(parBuffer.Length, Allocator.Persistent); } else { AgentAtCellQuantity = new NativeArray <int>(m_CellGroup.Length, Allocator.Persistent); } }
private void Cache() { var colorList = new List <Color>(); colorList.Add("#3ACF62".ToColor()); colorList.Add("#F7F0BB".ToColor()); colorList.Add("#F8165F".ToColor()); foreach (var color in colorList) { render = new MeshInstanceRenderer { castShadows = ShadowCastingMode.On, receiveShadows = true, material = new Material(material) { enableInstancing = true, color = color, }, mesh = mesh, subMesh = 0, }; RenderList.Add(render); } }
static void OnAfterSceneLoadRuntimeMethod() { TurretBodyLook = GetLookFromPrototype("bodyproto"); TurretHeadLook = GetLookFromPrototype("headproto"); TurretGun1Look = GetLookFromPrototype("gun1proto"); TurretGun2Look = GetLookFromPrototype("gun2proto"); Enemy1BodyLook = GetLookFromPrototype("EnemyBodyProto"); MissileLook = GetLookFromPrototype("missileproto"); var entityManager = World.Active.GetOrCreateManager <EntityManager>(); Entity spawnPoint = entityManager.CreateEntity(SpawnPointArchetype); entityManager.SetComponentData(spawnPoint, new EnemySpawnPoint { GridIndex = new int2(23, 10) }); Entity goalPoint = entityManager.CreateEntity(GoalPointArchetype); entityManager.SetComponentData(goalPoint, new EnemyGoalPoint { GridIndex = new int2(0, 10) }); playerDataEntity = entityManager.CreateEntity(PlayerDataArchetype); entityManager.SetComponentData(playerDataEntity, new PlayerSessionData { CurrencyAmount = 10, Score = 0, Health = 10, gameState = GameState.START }); UpdateHUDSystem.SetupGameObjects(World.Active.GetOrCreateManager <EntityManager>()); }
private void spawn_internal(EntityCommandBuffer.Concurrent entity_command_buffer, float current_time, ref float3 position, float rotation1) { entity_command_buffer.CreateEntity(arche_type_); entity_command_buffer.SetComponent(new Position { Value = position, }); entity_command_buffer.SetComponent(new Rotation { Value = quaternion.axisAngle(new float3(0f, 0f, 1f), rotation1), }); entity_command_buffer.SetComponent(new AlivePeriod { start_time_ = current_time, period_ = 0.8f, }); entity_command_buffer.SetComponent(new StartTime { value_ = current_time, }); var renderer = new MeshInstanceRenderer { mesh = mesh_, material = material_, subMesh = 0, castShadows = UnityEngine.Rendering.ShadowCastingMode.Off, receiveShadows = true, }; entity_command_buffer.SetSharedComponent(renderer); }
private MeshInstanceRenderer CreateMesh() { var meshRenderer = new MeshInstanceRenderer(); meshRenderer.mesh = _mesh; meshRenderer.material = _material; return(meshRenderer); }
public static void InitWithScene() { planetRenderer = GameObject.FindObjectOfType <MeshInstanceRendererComponent>().Value; for (int i = 0; i < 40000; i++) { SpawnPlanet(); } }
public static void InitWithScene() { // look in the hierarchy for a MeshInstanceRendererComponent and grab value of it planetRenderer = GameObject.FindObjectOfType <MeshInstanceRendererComponent>().Value; for (int i = 0; i < 400; i++) { SpawnPlanet(); } }
private static void LoadPrototypes() { var renderer = GetRendererFromPrototype("ProjectileRenderPrototype"); if (renderer.HasValue) { ProjectileRenderer = renderer.Value; } }
private void Start() { EntityManager EntityManager = World.Active.GetOrCreateManager <EntityManager>(); SandArchetype = EntityManager.CreateArchetype(typeof(Sand), typeof(Position), typeof(TransformMatrix), typeof(MeshInstanceRendererComponent)); SandRenderer = SandMesh.Value; Destroy(SandMesh.gameObject); }
public static void InitializeWithScene() { var settingsGO = GameObject.Find("Settings"); Settings = settingsGO.GetComponent <TestSettings>(); CubeLook = GetLook("CubeRender"); NewGame(); }
private void createMeshInstance() { var GO = GameObject.Instantiate(bulletPrefab); msi = new MeshInstanceRenderer(); msi.material = GO.GetComponent <Renderer>().material; msi.mesh = GO.GetComponent <MeshFilter>().mesh; Destroy(GO); }
// ---------------------------------------------------- #region // Private Functions /// <summary> /// MeshInstanceRendererの生成 /// </summary> /// <returns>生成したMeshInstanceRenderer</returns> public MeshInstanceRenderer CreateMeshInstanceRenderer(Material mat) { var matInst = new Material(mat); var meshInstanceRenderer = new MeshInstanceRenderer(); meshInstanceRenderer.mesh = this._mesh; meshInstanceRenderer.material = matInst; return(meshInstanceRenderer); }
public static void InitializeWithScene() { var renderer = GameObject.FindObjectOfType <MeshInstanceRendererComponent>(); if (renderer != null) { cubeRenderer = renderer.Value; } }
public static Collider getColliderInfo(MeshInstanceRenderer renderer) { Collider collider = new Collider { size = renderer.mesh.bounds.size.x / 2 }; return(collider); }