nine-slice-object.rst 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. .. include:: ../_header.rst
  2. NineSlice
  3. ---------
  4. The `Nine Slice <nine-slice-object.html>`_ is a built-in Phaser_ object type: `Phaser.GameObjects.NineSlice <https://photonstorm.github.io/phaser3-docs/Phaser.GameObjects.NineSlice.html>`_. This type allows you to display a texture-based object that can be stretched both horizontally and vertically, but that retains fixed-sized corners.
  5. A `Nine Slice`_ is created in code using the `nineSlice factory <https://photonstorm.github.io/phaser3-docs/Phaser.GameObjects.GameObjectFactory.html#nineSlice__anchor>`_. This is how the |SceneCompiler|_ generates the code of a `Nine Slice`_:
  6. .. code::
  7. const nineslice = this.add.nineslice(
  8. 0, 600, // x, y
  9. "ui", "green-button", // texture, frame
  10. 300, 200, // width, height
  11. 70, 70, // leftWidth, rightWith
  12. 60, 60); // topHeight, bottomHeight
  13. To create a `Nine Slice`_ object in the scene, you should follow these steps:
  14. 1. Drop a `NineSlice built-in block <blocks-view-integration.html>`_ on the scene:
  15. .. image:: ../images/scene-editor-create-9slice-1-20230208.webp
  16. :alt: Drop a nine slice object to the scene
  17. 2. Select a texture for the object by pressing `X` or opening the Texture Dialog in the Texture section:
  18. .. image:: ../images/scene-editor-nineslice-create-2-20230208.webp
  19. :alt: Select texture
  20. .. image:: ../images/scene-editor-nineslice-create-3-20230208.webp
  21. :alt: The result of selecting the texture
  22. 3. Change the size of the object. In the `Size section <size-properties.html>`_, you can set the width and height of the object. Also you can use the `Resize tool <./manipulation-tools.html#resize-tool>`_ (press the ``Z`` key):
  23. .. image:: ../images/scene-editor-nineslice-create-4-20230208.webp
  24. :alt: Resize the object
  25. 4. In the `Nine Slice properties`_ section, change the size of the slices. Also you can activate the `Nine Slice tool <manipulation-tools.html#slice-tool>`_, and change the sizes in the scene:
  26. .. image:: ../images/scene-editor-nineslice-create-5-20230208.webp
  27. :alt: Edit the slice size.
  28. By the way, you can `convert an Image or Sprite or any other game object into a Nine Slice <replace-object-type.html>`_.
  29. Nine Slice properties
  30. ~~~~~~~~~~~~~~~~~~~~~
  31. The `Nine Slice`_ type shares properties with other object types:
  32. * `Variable properties <variable-properties.html>`_
  33. * `Lists properties <lists-properties.html>`_
  34. * `Transform properties <transform-properties.html>`_
  35. * `Origin properties <origin-properties.html>`_
  36. * `Size properties <size-properties.html>`_
  37. * `Visible property <visible-property.html>`_
  38. * `Alpha single property <alpha-single-property.html>`_
  39. * `Tint properties <tint-properties.html>`_
  40. * `Texture property <texture-property.html>`_
  41. But also it has particular properties:
  42. .. image:: ../images/scene-editor-nine-slice-properties-20230208.webp
  43. :alt: Nine Slice properties.
  44. * The `leftWidth <https://photonstorm.github.io/phaser3-docs/Phaser.GameObjects.NineSlice.html#leftWidth__anchor>`_ and `rightWidth <https://photonstorm.github.io/phaser3-docs/Phaser.GameObjects.NineSlice.html#rightWidth__anchor>`_ properties. It changes the size of the horizontal slices.
  45. * The `topHeight <https://photonstorm.github.io/phaser3-docs/Phaser.GameObjects.NineSlice.html#topHeight__anchor>`_ and `bottomHeight <https://photonstorm.github.io/phaser3-docs/Phaser.GameObjects.NineSlice.html#bottomHeight__anchor>`_ properties. It changes the size of the vertical slices.
  46. Three Slice mode
  47. ~~~~~~~~~~~~~~~~
  48. The NineSlice object can be created with only three horizontal slices. This is an optimization for objects you only need to stretch horizontally. This optimization is activated if you set the **top** and **bottom height** to ``0``.
  49. In Phaser Editor 2D, we provide a **ThreeSlice** blocks for creating this particular configuration of the Nine Slice object. You can drop this block into the scene for creating the object:
  50. .. image:: ../images/scene-editor-threeslice-create-1-20230208.webp
  51. :alt: Create a 3 slice object.
  52. When the Nine Slice object is in the 3-slices mode, you can change the size of the slices with the `Slice Tool <manipulation-tools.html#slice-tool>`_ and the **Three Slice properties** section. Note it only shows the width properties and manipulators:
  53. .. image:: ../images/scene-editor-three-slice-create-2-20230208.webp
  54. :alt: Changes the width of the slices.