Unity Dots UI Communication

Date: 2023-8-1

A fan left a message at noon and asked, how does Ui communicate?

After I got home and took a shower, I remembered it and I will write about it.

First, communication is in the form of events and then designed through appearance patterns.

The event involves registration, so how to get the registered object?

The S in our ECS refers to System, which is generally inherited from SystemBase and ISystem

How to get the controller from the outside?

First write a concept (the blogger’s own name, not official),

Externally, we call it Mono, internally we call it ECS

first,

1. We establish the appearance class of ECS,

ECS_Facade, written as follows

The point is, inherited from SystemBase

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;

public partial class ECS_Facade : SystemBase
{
	public void GamePause()
	{
		Debug.Log("ECS Pause");
		// EntityManager logic
	}
	public void GameRestart()
	{
		Debug.Log("ECS Restart");
		// EntityManager logic
	}

	protected override void OnUpdate()
	{
		
	}
}

2. We create the appearance class of Mono

Mono_Facade

using UnityEngine;
using Unity.Entities;
using System;

public class Mono_Facade : MonoBehaviour
{
    public Action GamePause;
	public Action GameRestart;
	// Start is called before the first frame update
	void Start()
    {
		ECS_Facade ecsFacade =  World.DefaultGameObjectInjectionWorld.GetExistingSystemManaged<ECS_Facade>() ;

        GamePause  = ecsFacade.GamePause;
		GameRestart = ecsFacade.GameRestart;
	}

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.P))
        {
            GamePause?.Invoke();
		}
		if (Input.GetKeyDown(KeyCode.R))
		{
			GameRestart?.Invoke();
		}
	}
}

3. Notes on details

The System in ECS runs in the concept of a World.

So, get System through World.GetExistingSystemManaged

Let’s take a look again, why do we have to inherit from SystemBase?

Just take a look at generic constraints and System inheritance, and you’ll understand, right?

 

4. Author’s words

The first thing I do at the beginning of every blog post is the date.

Dots is a system that is updated very quickly. I also recommend that everyone use the newer version to learn and develop.

In the 2022.3 LTS version, Unity launched Dots1.0, which is considered the official version.

Don't study 0.5 pre or other under-development versions anymore. The functions are unstable, and the concepts and APIs are also changing.

Dots1.0 also provides a more user-friendly Bake system and more convenient editor tools.

Guess you like

Origin blog.csdn.net/qq_35623058/article/details/132052951