Sparrow . Unity

by Mr-Alan

Sparrow Framework (Unity Client Framework ) For Unity3D 5.6 or Later.

( Crawled 16 minutes ago )
+

Sparrow.Unity summary

Builtin features.

  1. Behaviour: Which can make your class like mono behaviour.
  2. Timer: Which can manage gameworld & realworld time.
  3. EventInterface: Which has some built-in event interface in the framework' life circle.
  4. Component: Which is mapping of business architecture.
  5. Module: Which is Providing services to components.
  6. Service: Which is Providing atomic services to modules.
  7. IOC: Which Provides configurable containers.

How to use Sparrow.Unity ?

The way to use sparrow.unity is App.GetComponent<T>();, It substance is still a normal unity component, But the underlying implementation relies on the service delivery architecture.

Components.

  1. FsmComponent: Provides creation, access, and destruction of finite state machines services.
  2. EventComponent: Provides event throwing, listening, and decoupling of projects services.
  3. ObjectPoolComponent: Provides effective management resources and resource reuse services.
  4. ProcedureComponent: Provides projects to be divided into processes services.

Click Sparrow.Core to learn more.

How to use the built-in features ?

Using Behaviour. ```csharp internal sealed class BehaviourTest : MonoBehaviour { private void Start() { App.Behaviour.RegisterBehaviour(new Dog()); } }

public sealed class Dog : ILogicalFrame
{
    public void OnBehaviourCompleted(IBehaviour behaviour)
    {

    }

    public void OnLogicalFrame()
    {
        Debug.Log("The fucking dog has been barking ....");
    }
}
>Using Timer.
```csharp
    internal sealed class TimerTest : MonoBehaviour
    {
        private void Start()
        {
            Timer.TimeToContinueTask("Test",1f,()=> {

                Debug.Log("Miao.......");

            });
        }
    }

How to customize ?

Custom Service.

    public interface IJsonParser : IService
    {
        object ToObject(string json,Type type);  
    }

Custom Module.

    public interface IDataParseModule : IModule
    {
        object FromFile(string filePath, Type type);
    }

    internal sealed class DataParseModule : IDataParseModule
    {
        private IServiceProvider m_ServiceProvider = null;
        private IFile m_File = null;
        private IJsonParser m_JsonParser = null;
        public void OnModuleInit(IServiceProvider serviceProvider)
        {
            m_ServiceProvider = serviceProvider;
            m_File = m_ServiceProvider.GetService<IFile>();
            m_JsonParser = m_ServiceProvider.GetService<IJsonParser>();
        }
        public void OnModuleDestroy()
        {
            m_File = null;
        }
        public object FromFile(string filePath, Type type)
        {
            var targetString =  m_File.ReadFile(filePath);
            return m_JsonParser.ToObject(targetString, type);
        }
    }

Custom Component.(ps: Component must be mounted under the [Sparrow] GameObject.)

    public interface IDataParseModule : IModule
    {
        object FromFile(string filePath, Type type);
    }

    [DisallowMultipleComponent]
    [AddComponentMenu("Sparrow/Event")]
    internal sealed class DataPaserComponent : MonoBehaviour, IComponent
    {
        private IThreadPoolModule m_ThreadPoolModule = null;
        private IDataParseModule m_DataParseModule = null;
        private IThreadObject m_ThreadObject = null;

        public void OnInit(IModuleProvider moduleProvider)
        {
            m_ThreadPoolModule = moduleProvider.GetModule<IThreadPoolModule>();
            m_DataParseModule = moduleProvider.GetModule<IDataParseModule>();
            m_ThreadObject = m_ThreadPoolModule.CreateThreadObject("ParserComponentTest",10);
        }

        public void PaseFromFile(string filePath,Type type,Action<object> handler)
        {
            m_ThreadObject.Task(to => {
                var @object = m_DataParseModule.FromFile(filePath, type);
                to.SyncState(@object);
            },
            null,
            syncList => {
                if (null != handler)
                {
                    handler.Invoke(syncList[0]);
                }
            });
            m_ThreadObject.Start();
        }

    }

The last thing that matters is the configuration.

    internal sealed class Test3rdModuleConfigured : IOnModuleConfigured
    {
        public void OnConfigured(IModuleRegister moduleRegister)
        {
            moduleRegister.Register<DataParseModule,IDataParseModule>();
        }
    }

    internal sealed class Test3rdServiceConfigured : IOnServiceConfigured
    {
        public void OnConfigured(IServiceRegister serviceRegister)
        {
            serviceRegister.Register<JsonParser,IJsonParser>();
        }
    }