Exemplo n.º 1
0
    public static void expose <T> (string propertyName, Func <object> getter, Func <T, object> setter, object[] allowedValues = null)
    {
        // TODO if already exposed throw error

        object value = prepareValue(getter());

        if (allowedValues != null)
        {
            allowedValues = (object[])prepareValue(allowedValues);
        }

        CapiType type = CapiType.STRING;

        if (typeof(T) == typeof(float))
        {
            type = CapiType.NUMBER;
        }
        else if (typeof(T) == typeof(String))
        {
            type = CapiType.STRING;
        }
        else if (typeof(T) == typeof(Boolean))
        {
            type = CapiType.BOOLEAN;
        }
        else if (typeof(T) == typeof(string[]))
        {
            type = CapiType.ARRAY;
        }
        else
        {
            // TODO throw type not supported exception
        }

        var res = GameObject.Find("CAPI");

        if (res == null)
        {
            var capiGO = new GameObject("CAPI");
            capiGO.AddComponent <CapiBehaviour> ();
        }

        Capi.initializeExpose();

        capiMetadata.Add(propertyName, new CapiMetadata(value, type, getter, (v) => { return(setter((T)v)); }));

        Application.ExternalCall("receiveExposeFromUnity", new object[] { propertyName, (int)type, value, allowedValues });
    }