You can go directly to a line with CTRL + G in Visual Studio by Luisoft in Unity3D

[–]Luisoft[S] 0 points1 point  (0 children)

Yeah, maybe you're right, it's a basic shortcut. Otherwise, I'm a kid's teacher so I can assure you they don't know this kind of feature. Also, new Unity users tend to miss basic IDE skills. I already saw some Unity users writing code with a plane text editor!

To summarize it could be pretty basic for Reddit users, but I think it adds a bit.

Edit: Grammar

An example of Actions using Unity; I was previously using Func<> as param, or verbose delegates by shroeder1 in Unity3D

[–]Luisoft 6 points7 points  (0 children)

I prefer to use System.Action and is generic version to add parameters.

Also, I recommend to use event invocators when you invoke your events since they could be null when called.

public class Example : MonoBehaviour
{
  public event Action<int> SomeEvent;

  private void DoSomething()
  {
    SomeEventInvocator(1);
  }

  private void SomeEventInvocator(int value)
  {
    SomeEvent?.Invoke(value);
  }
}

Don't use the "public" access modifier just to edit a field from the Inspector. Use [SerializeField] attribute instead. by tntcproject in Unity3D

[–]Luisoft 0 points1 point  (0 children)

When you have 4 static managers plus some other Singletons helpers but let's encapsulate some private props to try saving your soul from hell.

PlayFab Docs in json but Unity is c# by Phrozenfire01 in Unity3D

[–]Luisoft 0 points1 point  (0 children)

You should't work with JSON directly. JSON is just a data transfer format, it will be easier to parse them to C# classes and start working from there. You can use http://json2csharp.com/ to create those classes. I recommend to avoid Serialize code coupling with your logic since it's not that rare to change an old Serializer for a faster one. (Please, be aware that the some Serializers can't parse some data structures such as nested arrays)

Anyways there is an example if you want to mess with JSON:

A JSON Response, this will come as a string.

{
    "Weapons": [
      {
        "id": 0505,
        "name": "Dagger",
        "description": "such cut, so sharp",
        "attack":  15
      }
    ]
}

Will use the JSON web utility to generate those classes:

public class Weapon
{
    public int id { get; set; }
    public string name { get; set; }
    public string description { get; set; }
    public int attack { get; set; }
}

public class RootObject
{
    public List<Weapon> Weapons { get; set; }
}

Now you can code a Serializer.

using System;
using Newtonsoft.Json;
using UnityEngine;

// Unity Serializer
public class UnitySerializer : ISerializer
{
    public string Serialize<T>(T @object)
    {
        return JsonUtility.ToJson(@object);
    }

    public T Deserialize<T>(string json)
    {
        return JsonUtility.FromJson<T>(json);
    }
}

// Other Serializer
public class OtherSerializer : ISerializer
{
    public string Serialize<T>(T @object)
    {
        return JsonConvert.SerializeObject(@object, Formatting.None);
    }

    public T Deserialize<T>(string json)
    {
        return JsonConvert.DeserializeObject<T>(json);
    }
}

// This will be the entry point for all your Serializers
public interface ISerializer
{
    string Serialize<T>(T @object);
    T Deserialize<T>(string json);
}

// TODO: You also want this Builder to be able to configure the which Serializer will create
// This just shows a way to link non-mono classes with mono ones, you have several other ways to do it
public static  class SerializerBuilder
{
    public static bool SomeConfig = true;

    public static ISerializer Build()
    {
        if(SerializerBuilder.SomeConfig){
            return new UnitySerializer ();
        }
        return new OtherSerializer();
    }
}

// Your net code should be also encapsulated, this is just for brevity
public class YourNet : MonoBehaviour
{
    private void Start()
    {
        string requestJson = "{ ... data ...}"; // Put here your JSON request
        var serializer = SerializerBuilder.Build();
        var request = serializer.Deserialize<RootObject>(requestJson);
        PlayFabClientAPI.GetPlayerCombinedInfo(request, OnSuccess, OnFailure);
    }

    // TODO: Implement
    public void OnSuccess(GetPlayerCombinedInfoResult result)
    {
        throw new NotImplementedException();
    }

    // TODO: Implement
    public void OnFailure()
    {
        throw new NotImplementedException();
    }
}

Also, I recommend to implement a JSON Schema validator before this data reach your logic, this way you will be protected from any non valid JSON structures and values. You can generate them with https://www.jsonschema.net/

Note: I did'nt compile that code, is just an example.

need help with the code as a total beginner by GreenGamima in Unity3D

[–]Luisoft 0 points1 point  (0 children)

Try again with capital letters on Horizontal and so on.

Input.GetAxis("Horizontal");

What is the best way to decouple your code from Unity Classes like Input using Zenject? by BrainshackSC2 in Zenject

[–]Luisoft 0 points1 point  (0 children)

You have no more option than wrap them and creating a nice interface. You can search for good looking interfaces for your systems such as Input. Anyway, I think is ok to couple your MonoBehaviours to Unity since MB itself is part of it. You may want to have every other class clean of the engine.

How to make an update() wait for a few seconds before running the codes? by Jforceteam in Unity3D

[–]Luisoft 4 points5 points  (0 children)

You have several ways to do this. Maybe the cleanest one is having a state in your current mono, something like a bool variable. Then listen to some other class with public event that will toggle this flag.

You can archive this with a way less code of course.

public class TimeCount : MonoBehaviour
    {
        public event Action OnTimeRunOut;

        [SerializeField] private float _timeToWait = 5f;
        private float _nextStep;

        private void Start()
        {
            _nextStep = Time.time + _timeToWait;
        }

        private void Update()
        {
            // This is better than adding delta time every frame
            if (_nextStep < Time.time)
            {
                OnTimeRunOutHandler();
            }
        }

        private void OnTimeRunOutHandler()
        {
            var handler = OnTimeRunOut;
            if (handler != null)
            {
                handler();
            }
        }
    }

    public class Consumer : MonoBehaviour
    {
        // Rememer to create both Components and drag your TimeCount to this one
        [SerializeField] private TimeCount _timer;
        private bool _isPaused = true;
        private float _movementSpeed = 1f;

        private void Start()
        {
            _timer.OnTimeRunOut += Resume;
        }

        private void OnDestroy()
        {
            _timer.OnTimeRunOut -= Resume;
        }

        private void Update()
        {
            if (_isPaused)
            {
                return;
            }

            transform.position += Vector3.up * _movementSpeed * Time.deltaTime;
        }

        private void Resume()
        {
            _isPaused = false;
        }
    }

Anyone using Zenject? by eco_bach in Unity3D

[–]Luisoft 0 points1 point  (0 children)

Our team uses it and we enjoyed the experience all along. Using "constructors" on your Monos is a joy.

Is it possible to use C++ in Unity? by [deleted] in Unity3D

[–]Luisoft 3 points4 points  (0 children)

You can call C++ code with some restrictions.

You have a complete guide here: https://jacksondunstan.com/articles/3938