Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

330
Visualizações
Referencing using & and defreferencing using * in Unity

This is a C variable referencing question in Unity and not a gameobject reference question.

I need to get the address of newVar, as in &newVar. Is this possible in Unity? I have tried a number of searches and cannot find an answer.

There is documentation about how to do this in C but I need to do it in Unity.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I've never used Archimatix but looking at the manual I see online, it looks like this will work. In c#, lambdas capture variables by reference, so you can have the params read the result from a lambda to get an up-to-date variable.

public class ParamUpdater: MonoBehaviour {

    [SerializeField] AXModel model;

    List<AXParameter> params;
    List<Func<float>> paramGetters;

    // Use this for initialization
    void Awake () {

        // Establish refernces to AXModel parameters.

        Params = new List<AXParameter>();
        if (model != null)
        {
            Params.Add(model.getParameter("Engine.Param0"));
            Params.Add(model.getParameter("Engine.Param1"));
            // add more params here
        }

        paramGetters = new List<Func<float>>();
        
        // create default getters
        for (int i = 0 ; i < Params.Count ; i++)
        {
            paramGetters.Add(()=>0f);
        }
    }

    public void ConfigureParamGetter(int index, Func<float> getter)
    {
        paramGetters[index] = getter;
    }
  
    public void Recalculate()
    {
        for(int i = 0 ; i < Params.Count ; i++)
        {
            float curVal = paramGetters[i]();

            // Maybe a different method would be better? 
            // online API is very lacking...
            Params[i].initiatePARAMETER_Ripple_setFloatValueFromGUIChange(curVal);
        }  

        model.autobuild();
        recalculate();
    }
}

And then for example in a class where a float of interest lives, call ConfigureParamGetter with a Func that returns the variable. As mentioned before, the capture is done by reference, so it will always be up to date when called in ParamUpdater.

public class ImportantClass: MonoBehaviour
{
    [SerializeField] ParamUpdater paramUpdater;
 
    float importantFloat;
   
    // Use Start to call after ParamUpdater initializes in Awake
    void Start()
    {
        paramUpdater.ConfigureParamGetter(0, ()=>importantFloat);
    }

    // for example
    void Update() { importantFloat = Time.time; }
   
    // called whenever
    void DoRecalculate()
    {
        paramUpdater.Recalculate();
    }
}

Let me know if I made any syntax errors or any other problems, I can try and fix 'em.

over 4 years ago · Santiago Trujillo Relatório

0

You require Pointers and the unsafe code keyword.

Classes in C# are already references, so not much use converting to pointers. IE a GameObject is already a reference type, and it already works like a pointer.

For other things, such as unmanaged types, you might use unsafe code and pointers.

So you can do things such as char* pc = &c;

Also remember to "Allow unsafe code" with File > Build Settings... => Click Player Settings => Code Setting => Allow 'unsafe' code.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda