mirror of
https://github.com/godotengine/godot-docs.git
synced 2026-01-05 22:09:56 +03:00
Merge pull request #1380 from KellyThomas/resources
add c sharp samples to step_by_step/resources
This commit is contained in:
committed by
mhilbrunner
parent
b75af9f2c5
commit
f04f88e761
@@ -82,21 +82,36 @@ Loading resources from code
|
||||
Loading resources from code is easy. There are two ways to do it. The
|
||||
first is to use load(), like this:
|
||||
|
||||
::
|
||||
.. tabs::
|
||||
.. code-tab:: gdscript GDScript
|
||||
|
||||
func _ready():
|
||||
var res = load("res://robi.png") # resource is loaded when line is executed
|
||||
get_node("sprite").texture = res
|
||||
|
||||
.. code-tab:: csharp
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
var texture = (Texture)GD.Load("res://robi.png"); // resource is loaded when line is executed
|
||||
var sprite = (Sprite)GetNode("sprite");
|
||||
sprite.Texture = texture;
|
||||
}
|
||||
|
||||
The second way is more optimal, but only works with a string constant
|
||||
parameter, because it loads the resource at compile-time.
|
||||
|
||||
::
|
||||
.. tabs::
|
||||
.. code-tab:: gdscript GDScript
|
||||
|
||||
func _ready():
|
||||
var res = preload("res://robi.png") # resource is loaded at compile time
|
||||
get_node("sprite").texture = res
|
||||
|
||||
.. code-tab:: csharp
|
||||
|
||||
// preload() is unavailable in C Sharp
|
||||
|
||||
Loading scenes
|
||||
--------------
|
||||
Scenes are also resources, but there is a catch. Scenes saved to disk
|
||||
@@ -107,12 +122,23 @@ To obtain an instance of the scene, the method
|
||||
:ref:`PackedScene.instance() <class_PackedScene_instance>`
|
||||
must be used.
|
||||
|
||||
::
|
||||
.. tabs::
|
||||
.. code-tab:: gdscript GDScript
|
||||
|
||||
func _on_shoot():
|
||||
var bullet = preload("res://bullet.tscn").instance()
|
||||
add_child(bullet)
|
||||
|
||||
.. code-tab:: csharp
|
||||
|
||||
private PackedScene _bulletScene = (PackedScene)GD.Load("res://bullet.tscn");
|
||||
|
||||
public void OnShoot()
|
||||
{
|
||||
Node bullet = _bulletScene.Instance();
|
||||
AddChild(bullet);
|
||||
}
|
||||
|
||||
This method creates the nodes in the scene's hierarchy, configures
|
||||
them (sets all the properties) and returns the root node of the scene,
|
||||
which can be added to any other node.
|
||||
|
||||
Reference in New Issue
Block a user