Kategorier: Alle - execution - interface - events

af Mike Ton 9 år siden

1314

C#

Coroutines in C# are useful for managing tasks that need to be executed over multiple frames without causing significant performance issues. By leveraging the IEnumerator interface, developers can create coroutines that yield control back to the main program, allowing for efficient time management and CPU utilization.

C#

C #

(source)

gCOREView.cs

jumpDNHandler(){ ... }

override

OnDisable(){ ... }

io_MTView.OnJumpDN -= jumpDNHandler;

//MUST : remove events on disable, else memory leaks and other catastrophes

OnEnable(){ ... }

io_MTView.OnJumpDN += jumpDNHandler;

//binding IO events

MonobehaviourClass.cs

public class Player : IHealth, IXform, IInput{...}

//implement IXform ....IHealth....etc

Vector3

position{ ... }

set{ ... }

xform.position = value;

//WTH is "value" coming from...keyword???

get{ ... }

return xform.position;

Transform

xform{ get; set; }

//not understanding {get; set; } template???

doMove(){ ... }

// insert normal function code here

//MUST : Else error

//All properties and methods must be implemented in importing class

//Interface property must be public???

(features)

(Events)
(Delegates)

//events vs. delegates : events more secure, can not be overwritten and adjusted by externals

io_MTView.cs

void Update(){ ... }

if(Input.GetButtonDown("Jump")){ ... }

OnJumpDN();

public

static

event

bnEVENT

OnJumpRL ;

//onJump Release

OnJumpDN ;

//onJump Press

delegate

void

bnEVENT() ;

(Interface)
(Abstract)

//Interface with default methods and properties ???

__InterfaceMT.cs

public interface IHealth{ ... }

public interface IXform{ ... }

(properties)

Vector3 position{ get; set; }

Transform xform{ get; set;}

(methods)

void doMove();

Coroutine
EX:

(cleanup)

public override void GameOver(){ ... }

StopAllCoroutines();

public IEnumerator SpawnPipes(){ ... }

while(mtonFlapperGame.State == mtonFlapperState.Playing){ ... }

yield return new WaitForSeconds(mtonFlapperGame.PipeSpawnSpeed);

mtonFlapperGame.Pipes.Add(new PipeViewModel(PipeController));

(init)

public override void Play(){ ... }

StartCoroutine(SpawnPipes());

Coroutines are excellent when modelling behaviour over several frames. Coroutines have virtually no performance overhead

StartCoroutine function always returns immediately, however you can yield the result. This will wait until the coroutine has finished executio

The execution of a coroutine can be paused at any point using the yield statement. The yield return value specifies when the coroutine is resumed.

IEnumerator

The IEnumerator allows the program to yield things like the WaitForSeconds function, which lets you tell the script to wait without hogging the CPU