#Unity Arduino C# and Unamanged C++ structs

1 messages · Page 1 of 1 (latest)

tough steeple
#

Ayo @thorny dawn

thorny dawn
#

ye

tough steeple
#

okay one sec I'm gonna open the project rq

tough steeple
# thorny dawn ye

here are two example structs in c

struct XYZ {
  float x;
  float y;
  float z;
};
struct Person {
  char name[25];
  int age;
  XYZ xyz;
};```
`Person person;`
#
void writeDataToPort(){
  Serial.write((uint8_t*)&person, sizeof(person));
}```
#

then in c# (can use any frame work for UI)

   int structSize = Marshal.SizeOf(typeof(Person));
   byte[] buffer = new byte[structSize];```
```cs
  if (serialPort.BytesToRead > 0 && readData)
  {
      byte[] incomingBytes = new byte[serialPort.BytesToRead];
      serialPort.Read(incomingBytes, 0, incomingBytes.Length);
      var student = ByteArrayToStruct<Person>(incomingBytes);
      Console.WriteLine($"byte data length: {incomingBytes.Length}");
              ```
thorny dawn
#

ive never used a struct

tough steeple
thorny dawn
#

what does the * and & do on this

thorny dawn
tough steeple
thorny dawn
#

alr

tough steeple
#

it just bunches data together mainly

#

you used Vector3 in unity before no? or Quaternion

thorny dawn
#

ye

tough steeple
#

those are structs

#

Vector3 is just a struct of 3 floats

thorny dawn
#

ye i see

tough steeple
#

the reason is the structs are usually value types and handled different , in many cases its more efficient. Ideally most of the data is kept on the stack

thorny dawn
#

serialPort.Read(incomingBytes, 0, incomingBytes.Length);

#

whats the point of that

#

im assuming it outputs something because its not being saved in a variable

tough steeple
#

basically this is replacement for reading strings

#

instead of reading strings you are reading a specific bytes

thorny dawn
#

i see

#

and this is for wifi?

tough steeple
#

this would work over serialport should be similiar, have to revis the old project its been a while

#

the important part is processing that same data instead as string but raw data

thorny dawn
#

yeah

#

im still a little confused on what to use for bluetooth

#

you most likely dont know any libraries

#

but ive gotten over my immense stupidity now and without narrowing it down to maui itll probably be easier to find videos on

tough steeple
#

like I said, bluetooth or not the framework is irrelevant lol this would work across all .net frameworks

#

including Unity. Unity also uses .NET

#

if it works on Unity it works for any other platform on .NET maui/blazor included or like i said console app

thorny dawn
#

i could use unity to control the servos

#

i could make the gui on unity not much point though over an actual ui

tough steeple
#

yeah I mean if you write clean code it can work on all the platforms, if you want specific UI and look then yes thats where it makes a difference

#

Blazor - Html/CSS
Unity - Custom Editor / GUI Tools / UI Toolkit
MAUI - Native platform controls / Pain in the ass XAML

#

I like mostlly using Blazor cause I get to use JS and whatever library from that , as well as for CSS using things like Bootstrap or Tailwind

#

if its simple usecase and don't need 3D rendering or interaction heavy like unity

thorny dawn
#

dumb question but blazor still uses c# at its core and not js

#

my js is lacking

#

but hell yeah xaml is a pain

#

i would much rather do html

tough steeple
#

yes blazor uses something called Razor files that are HTML and C# combined basically

#

but because blazor is web, you can also call JS functions if you wanted to

#

or call C# functions from JS

thorny dawn
#

definitely using that

#

maui can die in a very bright fire

tough steeple
#

yeah its really being slept on

#

the easyness of c# but something as powerful as NextJS and other serverside frameworks.
Ofc Blazor can also use other frameworks for frontend like react or vue

#

and blazor can also do Client side apps so you don't need a server side rendering or you can do both (this is something you learn about later)

#

ServerSide is basically code that runs on another computer so people cannot decompile your code, this is especially important when handling back end stuff like Databases

#

Client side rendering you have to download the HTML/JS/WebAssembly document first then your local pc runs it (which opens up people seeing your code)

thorny dawn
#

but still works on its own

tough steeple
thorny dawn
#

its still web dependant though

#

so if you just wanted a standalone app without needing wifi

tough steeple
#

internet has nothing to do with it though

#

the only difference is you're running it in a browser vs not

#

also there is MAUI Hybrid that embeds the Blazor parts in the WebView

#

ultimately depends on what features you need and what you don't need

thorny dawn
#

im somewhat confused im slow sometimes mb

#

if it needs a browser surely that means not its own app?

#

or is it its own browser..?

tough steeple
#

yeah you need to distribute it like you would a regular website, but can be run locally too if you spin up a simple https server

#

MAUI hybrid makes it so you have it already embedded in the Naitive App

thorny dawn
#

isnt flutter also like maui hybrid

tough steeple
#

I think so, I haven't used flutter

thorny dawn
#

i should try doing something with servers or databases soon

#

ive never used them because i dont a need for either

tough steeple
#

they both do the same thing though they are both native apps

#

so yeah its easier to just use the c# equivalent network

#

albeit still infant

tough steeple
#

but yeah any other service also can work like AWS or Azure etc.

#

MongoDB is also nice

thorny dawn
#

maybe ill try make a login / user system for something with a database

#

i made a discord bot too which needs a server

tough steeple
#

Unity has one with Unity Auth which is pretty nice

#

I can login easy using Google account then access whatever from there

#

ofc ontop of the regular username/pass login

thorny dawn
#

what more is it other than meaning they cant use special characters and limiting the length etc any custom filtering / requirements

thorny dawn
#

for authentication

#

obviously the databasing stuff for accessing it