UnityHTTPServer icon indicating copy to clipboard operation
UnityHTTPServer copied to clipboard

Multiple Classes

Open Weaver1911 opened this issue 4 years ago • 3 comments

Hello there. 1st of all, i want to thank you about this tool. That's exactly what i wanted to create and i've failed -many times-. This is not a simple HTTP server. It's gem. But I'm trying to do something like this

 public MyClassCollection C1()
    {
        MyClassCollection result = new MyClassCollection
        {
            _Class2_data.MyFloat = 0.5f,
            _Class2_data.MyInt = 1,
            _Class2_data.MyString = "This is a test",
            
            _Class2_data._Class3_data.MyFloat1 = 0.5f,
            _Class2_data._Class3_data.MyInt1 = 1,
            _Class2_data._Class3_data.MyString1 = "This is a test 2"
        };
        return result;
    }

    public class MyClassCollection
    {
        public MyClass2 _Class2_data;

    }

    public class MyClass2
    {
        public float MyFloat;
        public int MyInt;
        public string MyString;
        public MyClass3 _Class3_data;
    }

    public class MyClass3
    {
        public float MyFloat1;
        public int MyInt1;
        public string MyString1;
    }

And seems to be impossible. Am i missing something? Or maybe Is there any other way i can use to achieve that result ?

In reality is use 3rd party software to put values there like this _Class2_data.MyFloat = MySoftwareOutput.value,

Thanks in advance.

Weaver1911 avatar Jan 14 '21 13:01 Weaver1911

Hi!

Are you wish to use "MyClassCollection" as a custom object return? The custom object return require the object which is supported by the JSON library your are using.

The SimpleHTTPServer use the Unity's JsonUtility as the default JSON library. According to the Unity's manual, JsonUtility based on the Unity's serialization, so maybe add [System.Serializable] attribute on your custom class makes it become serailizable will fix the problem.

In the other hand, have you try using LitJson library, and is there same problem when using LitJson? LitJson has better compatibility than JsonUtility on serialize to Json string, I thing the problem should not exist when using LitJson. (Add "UseLitJson" define symbol in your ProjectSetting and add LitJson.dll in Assets/Plugins folder)

If the problem still exist, could you please give me the console log, exception message or any error msg about issue. It will be a great help for fix.

sableangle avatar Jan 14 '21 15:01 sableangle

Hello sableangle.

sorry about that huge delay, I was really busy with other stuff.

Yes this is what i want to achieve. And yes i use JsonUtility by Unity. I don't exactly know what is the problem, Since i've already deserialized the Json and it's working as intended.

I've tried the LitJson too. Like this: Screenshot_3 But nothing changed. still the same errors were there. Screenshot_4

And this is how the errors in my code looks like Screenshot_1 Screenshot_2

I really don't understand what to do. Hope that helps you understand what i'm doing wrong. I'm not experienced with Json that much.

Weaver1911 avatar Feb 08 '21 20:02 Weaver1911

try offical new netwtonjson libray might be useful:https://docs.unity3d.com/Packages/[email protected]/manual/index.html

orangeagain avatar Apr 15 '22 19:04 orangeagain