Unity Monobehaviour class vs Normal non Monobehaviour class?

Started by Amon, September 27, 2019, 00:21:01

Previous topic - Next topic

Amon

I want to make a 3d rasterbar in Unity like I did in agk. Here is the agk code.

// Project: RasterBar3D
// Created: 2019-09-25

// show all errors
SetErrorMode(2)

// set window properties
SetWindowTitle( "RasterBar3D" )
SetWindowSize( 768, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window

// set display properties
SetVirtualResolution( 768, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 60, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts


SetCameraPosition(1, -15, 0, -20)
SetCameraRotation(1, 0, 45, 0)
type Bar
    object as integer
    x as float
    y as float
    z as float
    angle as float
endtype
global BarList as Bar[]

TempZ = 0
TempAngle = 0
for xi = 0 to 10
    b as Bar
    b.object = CreateObjectBox(10, 1, 1)
    SetObjectColor(b.object, 255,0,0, 1)
    SetObjectCastShadow(b.object, 1)
    SetObjectReceiveShadow(b.object, 1)   
    b.x = 0
    b.y = 0
    b.z = TempZ
    b.angle = TempAngle
    TempZ = TempZ - 1.1
    TempAngle = TempAngle + 20.0
    BarList.insert(b)
next
   



do
    if BarList.length <> -1
        for xi = 0 to BarList.length
            SetObjectPosition(BarList[xi].object, BarList[xi].x, BarList[xi].y, BarList[xi].z)
            BarList[xi].y = BarList[xi].y + Cos(BarList[xi].angle) * 1 / 10.0
            BarList[xi].angle = BarList[xi].angle + 2.0
        next
    endif
    Print( ScreenFPS() )
    Sync()
loop

Now, in unity how would I do this? I understand I could have a normal c# class that does this but if it does not derive from monobehaviour then later on if I want to use the unity goodies like collision etc I won't be able to.
In a normal c# class I can use a constructor to create instances using 'new' much like in monkey-x but this can't be done if your class derives from a monobehaviour.
I havent used unity in any decent depth for a long while so I could do with some help.

Qube

The way I'd do it as a quick thing would be ( not the most optimised but it'd work - probably would need mono behaviour though ) :

1.. Create a cube the desired parameters you want.
2.. Give that cube a tag called "cube", add a box collider and then make that cube a prefab.
3.. Delete the cube from the scene.
4.. Instantiate as many cube prefabs as needed.
5.. Search for the tag and manipulate as required.
Mac Studio M1 Max ( 10 core CPU - 24 core GPU ), 32GB LPDDR5, 512GB SSD,
Beelink SER7 Mini Gaming PC, Ryzen 7 7840HS 8-Core 16-Thread 5.1GHz Processor, 32G DDR5 RAM 1T PCIe 4.0 SSD
MSI MEG 342C 34" QD-OLED Monitor

Until the next time.

Amon

Yeah, I've done that already and it does work. I just wanted to use classes instead. I'm having problems in that if you have a unity class that derives from monobehaviour you can't use 'new' to create new instances like you can in a normal c# class that doesn't derive from monobehaviour.
I could instantiate objects but how do I access fields to assign values when instantiating?

Qube

I'm a Unity noob and my knowledge of it is super basic :P - But is this any help?
Mac Studio M1 Max ( 10 core CPU - 24 core GPU ), 32GB LPDDR5, 512GB SSD,
Beelink SER7 Mini Gaming PC, Ryzen 7 7840HS 8-Core 16-Thread 5.1GHz Processor, 32G DDR5 RAM 1T PCIe 4.0 SSD
MSI MEG 342C 34" QD-OLED Monitor

Until the next time.

Amon


Xaron

When using classes which derive from Monobehavior, you cannot use new to construct it.

Instead you're doing:

class MyClass : Monobehavior
{
  ...
}

...

// instantiation:
Gameobject cube = new Gameobject();
MyClass myclass = cube.AddComponent<MyClass>();