Unity3d parsing text executing a registered custom function

Source: Internet
Author: User
Tags parse string

Recently there is a need to let the program parsing and editing a text generated a piece of CG, the content is used in general

Cgsetcameraex (118.6324,30.71189,75.55666,45,-45,0,0) cgclonemyplayer (1) cgsetposdir (1,109,80,0) cgSetCameraEx ( 114.18,23.17,96.9,11.51974,-61.49661,-3.384705,3.5) Cgcreateactor (2,25,108,100,90) cgWait (0.2) CgMove ( 1,109.7,100,9,false) cgwait (3) cgfacetoactor (+) cgwait (1.5) Cgpoptalk (1,1,2)

  

My approach is to use a third-party library, Ncalc, to register the custom function with the parser and have the parser go to load the text

Using unityengine;using system.collections;using ncalc;using system.collections.generic;using System;// Parse string expression execution function public class Callfunctionscript {//cache function table Private readonly dictionary<string, func<expression[], object>> mregisterfunctions = new dictionary<string, func<expression[], object>> ();//Register the function that can be executed// The registered function format satisfies the public static object Func (expression[] args)//function return value cannot be null if the return value is not required to return 1public void Registerfunction (string FuncName, func<expression[], object> Func) {if (!mregisterfunctions.containskey (FuncName)) { Mregisterfunctions.add (FuncName, func);} Else{unityengine.debug.logerror ("duplicate func" + FuncName);}} Anti-registration all functions public void unrgisterallfunction () {mregisterfunctions.clear ();} Execute function public Object execute (string expression) {var exp = new expression (expression);//function Agent exp. Evaluatefunction + = delegate (string name, Functionargs args) {func<expression[], object> func;if    (Mregisterfunctions.trygetvalue (name, out func))        {object result = NULL; try {result = func (args.                Parameters); }catch (Exception ex) {UnityEngine.Debug.LogError ("Execute" +name + "() Error:" + ex.                ToString ());} finally {args.                result = result;        }}else{unityengine.debug.logerror ("Can ' t find function [" + name+ "]");}        }; try {return exp. Evaluate ();//Execute function} catch (Exception ex) {debug.logerror ("Evaluate \" + expression + "\" Error: "+ex .            ToString ());        return null; }}}

  

Functions that are registered as such

public static class Cgfunctionwrapper{public static object Cgclonemyplayer (expression[] args) {if (args. Length < 1) {Logger.error ("Cgclonemyplayer () args<[1]"); return 0;} PlayCG.Instance.cgCloneMyPlayer (Convert.ToInt32 (args[0). Evaluate ())); return 1;}}

  

How to use

Public Callfunctionscript mexecutor = new Callfunctionscript (); Mexecutor.registerfunction ("Cgclonemyplayer", Cgfunctionwrapper.cgclonemyplayer); Mexecutor.execute ("Cgclonemyplayer (1)");

  

Unity3d parsing text executing a registered custom function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.