Unity game framework to build 2018 (c) QFramework Quick Start

Foreword

QFramework development has been three years, so far, has not carried out a full introduction. The reason is that, in the past, QFramework in rapid iterations, changes in API is relatively large.

To this day, QFramework current version (v0.0.7) has stabilized, will not be much change, this is the opportunity to write this article.

This part describes the QFramework three core modules, namely:

  • Resource management module Res Kit
  • UI Kamachika UI Kit
  • Execution node system Action Kit

QFramework corresponding to the directory structure as follows:

image

QFramework version of this article used to v0.0.7 version, download the address given in the article tail.

Res Kit Quick Start

API ResKit users most frequently used only ResLoader this class. The word as meaning, ResLoader duty is to provide resources to load services.

For example, to load resources under the Resources directory, as follows:

// allocate a loader when initialize a panel or a monobehavour
var loader = ResLoader.Allocate();

// load someth in a panel or a monobehaviour
var smobjPrefab = loader.LoadSync<GameObject>("Resources/smobj");

var bgTexture = loader.LoadSync<Texture2D>("Resources/Bg");

var gameObjPrefab = loader.LoadSync("Resources/gameObj") as GameObject;


// resycle this panel/monobehaivour loaded res when destroyed 
loader.Recycle2Cache();
loader = null;

Code, there has been ResLoader.Allocate () This API. Allocate Chinese meaning is "application", meaning that a ResLoader application pool from one object container. We can directly be understood as new ResLoader ().

Its antithesis is code fragment loader.Recycle2Cache () this API, meaning that this object is ResLoader recovered.

loader.Recycle2Cache () does two things:

  • For ResLoader in load over resources once flush operation.
  • This recovered pool ResLoader objects to which the object container. You can directly destroy the object understood as operating just fine.

In addition, there ResLoader.LoadSync this API, literally, is synchronized to load. To enter their own generic type, such ResLoader.LoadSync <GameObject> ( "Resources / SomeObj");

The parameters passed, is the path to load the resource. If the prefix plus "Resources /", then loads the resources in the Resources directory. If not, then it is in the default resource loading AssetBundle. Here, how to load AssetBundle in resources?

To use AssetBundle, it must first be made of AssetBundle.

Production in the AssetBundle Res Kit is very simple.

ready:

  • Right-click a resource or folder, then select @ResKit Mark AssetBundle. This operation is the marking operation.
  • Shortcut Command / Ctrl + Shift + R pop-up resources to build panel, make sure Simulation Mode check box.

Such a AssetBundle preparation is complete.

Load AssetBundle resource in the code:

// init res mgr before load asset bundle
ResMgr.Init();

// allocate a loader when initialize a panel or a monobehavour
var loader = ResLoader.Allocate<ResLoader>();

// load someth in a panel or a monobehaviour
var smObjPrefab = loader.LoadSync<GameObject>("smObj");

var bgTexture = loader.LoadSync<Texture2D>("Bg");

var logoTexture = loader.LoadSync<Texture2D>("hometextures","logo");

// resycle this panel/monobehaivour loaded res when destroyed 
loader.Recycle2Cache();

loader = null;

Code Code and loading Resources resources are very similar.

The code can be found in the very beginning had a ResMgr.Init () operation.

This operation is generally invoked once, ResMgr.Init largely resolved AssetBundle and Asset configuration table when the game started. And this configuration is automatically generated in the production of AssetBundle, where not much to say, in the back of the article will explain succinctly.

Load the API or LoadSync, but is not the path of the incoming resources Resources / prefix. This API supports only incoming AssetName, unnecessary incoming AssetBundleName. If there is a resource the same name, it can be passed in the corresponding AssetBundleName.

In addition to supporting resources and AssetBundle load resources under the Resources directory, also supports a variety of paths and network resources.

OK, Res Kit Quick Start to introduce here.

UI Kit Quick Start

UI Kit is QFramework in UI development kit. It integrates the management UI, UI component mechanism can be accumulated and a set of simple logic separation performance architecture rules.

How to make a UI interface?

The first step: UIRoot dragged into the Hierarchy

image

Can be seen, Design is designed to interface with the fight, when the scenario is running, everything will be hidden under the Design node.

Step two: a good fight any interface, add UIMark want to get control of the script in the code.
image

The third step: the Panel made into prefab placed anywhere (recommended in Assets / Art / UIPrefab), the right-click the prefab, select @ResKit - Create UI Code to generate the UI code.

image

Generate code positions as follows:

image

The fourth step UI script writing

/****************************************************************************
 * 2018.6 凉鞋的MacBook Pro (2)
 ****************************************************************************/
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;

namespace QFramework.Example
{
    public class UIHomePanelData : UIPanelData
    {
        // TODO: Query Mgr's Data
    }

    public partial class UIHomePanel : UIPanel
    {
        protected override void InitUI(IUIData uiData = null)
        {
            mData = uiData as UIHomePanelData ?? new UIHomePanelData();
            //please add init code here
        }

        protected override void ProcessMsg (int eventId,QMsg msg)
        {
            throw new System.NotImplementedException ();
        }

        protected override void RegisterUIEvent()
        {
            BtnStartGame.onClick.AddListener(() =>
            {
                Log.E("开始按钮点击");
            });
        }

        protected override void OnShow()
        {
            base.OnShow();
        }

        protected override void OnHide()
        {
            base.OnHide();
        }

        protected override void OnClose()
        {
            base.OnClose();
        }

        void ShowLog(string content)
        {
            Debug.Log("[ UIHomePanel:]" + content);
        }

        UIHomePanelData mData = null;
    }
}

The fifth step of the test interface

  • Creating GameObject named TestUIHomePanel in the scene.
  • Hang UIPanelTester script, fill in the Panel name.

image

  • The prefab UI labeled AssetBundle.
  • run.

You can see the screen.

Of course, open the page with the code is relatively simple.

UIMgr.OpenPanel<UIHomePanel>();

OK, UIKit Quick Start wrote here.

ActionKit Quick Start

ActionKit is designed to solve management problems asynchronous logic, asynchronous logic is often more difficult to manage in the daily development, but also great differences in style code. Such as "play a sound to play and get the event completed," "is triggered when xxx is true", including our common Tween animation is asynchronous logic, or more asynchronous logic can be used ExecuteNode to encapsulate them. Thus devised NodeSystem , inspired by cocos2d of CCAction .

ActionKit There are two basic concepts Node and Action:

  • Action for the operation command.
  • Node for the Action container.

Foundation Node

1. Delay Node: DelayAction

By the this (MonoBehaviour) callback trigger delay.

this.Delay(1.0f, () =>
{
    Log.I("延时 1s");
});

By applying DelayNode object, use the this (MonoBehaviour) callback trigger delay.

var delay2s = DelayAction.Allocate(2.0f, () => { Log.I("延时 2s"); });
this.ExecuteNode(delay2s);

Use Update drive callback delay.

private DelayAction mDelay3s = DelayAction.Allocate(3.0f, () => { Log.I("延时 3s"); });

private void Update()
{
    if (mDelay3s != null && !mDelay3s.Finished && mDelay3s.Execute(Time.deltaTime))
    {
        Log.I("Delay3s 执行完成");
    }
}

FeatureId:CEDN001

2. Event node: EventAction

The word as meaning, eventAction , which is distributed events. Perhaps alone does not exert its value, but in the container node where he is indispensable.

By applying EventAction object, use the this (MonoBehaviour) trigger event execution.

var eventAction = EventAction.Allocate(() => { Log.I("event 1 called"); }, () => { Log.I("event 2 called"); });
this.ExecuteNode(eventAction);

Use Update drive callback.

private EventAction mEventAction2 = EventAction.Allocate(() => { Log.I("event 3 called"); }, () => { Log.I("event 4 called"); });

private void Update()
{
    if (mEventAction2 != null && !mEventAction2.Finished && mEventAction2.Execute(Time.deltaTime))
    {
        Log.I("eventAction2 执行完成");
    }
}

FeatureId:CEEN001

Base container

1.Sequence

SequenceNode word sequence as it is intended node is a container node may be performed according to the order the child nodes, each node performs a complete before the next node.

By the this (MonoBehaviour) callback trigger delay.

this.Sequence()
    .Delay(1.0f)
    .Event(() => Log.I("Sequence1 延时了 1s"))
    .Begin()
    .DisposeWhenFinished() // Default is DisposeWhenGameObjDestroyed
    .OnDisposed(() => { Log.I("Sequence1 destroyed"); });

By applying SequenceNode object, use the this (MonoBehaviour) trigger node execution.

    var sequenceNode2 = SequenceNode.Allocate(DelayAction.Allocate(1.5f));
    sequenceNode2.Append(EventAction.Allocate(() => Log.I("Sequence2 延时 1.5s")));
    sequenceNode2.Append(DelayAction.Allocate(0.5f));
    sequenceNode2.Append(EventAction.Allocate(() => Log.I("Sequence2 延时 2.0s")));

    this.ExecuteNode(sequenceNode2);

    /* 这种方式需要自己手动进行销毁
    sequenceNode2.Dispose();
    sequenceNode2 = null;
    */

    // 或者 OnDestroy 触发时进行销毁
    sequenceNode2.AddTo(this);

Use Update drive execution.

private SequenceNode mSequenceNode3 = SequenceNode.Allocate(
            DelayAction.Allocate(3.0f),
            EventAction.Allocate(() => { Log.I("Sequence3 延时 3.0f"); }));

private void Update()
{
    if (mSequenceNode3 != null 
        && !mSequenceNode3.Finished 
        && mSequenceNode3.Execute(Time.deltaTime))
    {
        Log.I("SequenceNode3 执行完成");
    }
}

private void OnDestroy()
{
    mSequenceNode3.Dispose();
    mSequenceNode3 = null;
}

OK, ActionKit Quick Start here

Advantage

Res Kit:

  • Easy to use.

  • Each ResLoader the reference count management resources, will not cause repeated loading, unloading and other issues repetition, the brain to reduce the load.
  • Simulation Mode Mode do not always need to Build AssetBundle, accelerate development, reduce time costs.
  • ResLoader only supports incoming AssetName, when the resource directory structure changes do not have to change the code, reduce labor costs.
  • In addition to management Resources and AssetBundles resources, but also supports loading of network resources and local resources, the interface can be customized, more flexible.
  • Other advantages with the common market AssetBundleManager.

UI Kit:

  • Easy to use.

  • Code generation, save time and avoid the transform.Find ( "XXX"). GetComponent A lot of the preparation of this API.
  • UIComponent UI space can be multiplexed and modular, easy to accumulate team UI controls.
  • And a set of performance data is recommended to use separate structure (non-MVC).
  • Other advantages with the common market UIManager.

Action Kit:

  • Easy to use.

  • Write complex asynchronous logic clearer way.
  • Scalable and organize all asynchronous operations Tween, Animation, network requests the like.
  • Since the composition may be a state machine.
  • Itself is a set of lightweight behavior tree.
  • Chain support.

In a subsequent article, we will file structure, module structure, Quick Start and recommended use of each module, and several other perspectives QFramework introduced.

OK, first here today.

Address reprint please specify: sandals notes: liangxiegame.com

more content

  • QFramework Address: https://github.com/liangxiegame/QFramework
  • QQ exchange group: 623 597 263
  • Unity advanced small class :
    • The main training content:
      • Framework set up training (first year)
      • Case study along with Shader (first year)
      • Sideline incubation (second year, third year)
    • Specific details of equity, in the form of lectures, etc. Please see the "small class Product Manual" : HTTPS: //liangxiegame.com/master/intro
  • No public concern: liangxiegame obtaining a first time update notifications, and more free content.

Guess you like

Origin www.cnblogs.com/liangxiegame/p/12467237.html