This document is about: FUSION 2
SWITCH TO

拡張

Partial Class(部分実装)

partial class実装によってKCCアドオンを拡張することは、元のソースコードを変更することなく既存のクラスの機能を拡張することができる強力なテクニックです。 新しい Class.Feature.cs または Class.Game.cs ファイルを作成し、ロジックを記述するだけです。これにより、アドオンのアップデートがより簡単になり、プロジェクトが整理されます。

以下のセクションでは、既存のクラスを拡張する方法を説明します。

KCC の拡張

以下の実装でKCC.Sprint.csファイルを作成します。

C#

namespace Fusion.KCC
{
    public partial class KCC
    {
        public void SetSprint(bool sprint)
        {
            Data.Sprint = sprint;
        }
    }
}

KCCDataの拡張

以下の実装で KCCData.Sprint.cs ファイルを作成します。

C#

namespace Fusion.KCC
{
    public partial class KCCData
    {
        public bool Sprint;

        partial void CopyUserDataFromOther(KCCData other)
        {
            // Making a deep copy for correct rollback from local history.
            // This method is executed when you get a new state from server.
            // It is also executed after fixed updates to copy fixed data to render data.

            Sprint = other.Sprint;

            // Because this method is partial and cannot be implemented for each property separately, you have to copy all properties here.
            // Or use this method as a wrapper and split execution into multiple calls.
        }
    }
}

⚠️ KCCData に追加されるすべてのプロパティは partial void CopyUserDataFromOther(KCCData other) にも追加してください。

KCCSettings の拡張

Create a file KCCSettings.CustomProperty.cs with following implementation:

C#

namespace Fusion.KCC
{
    public partial class KCCSettings
    {
        public bool CustomProperty;

        partial void CopyUserSettingsFromOther(KCCSettings other)
        {
            // Make a deep copy of your properties.
            // This method is also executed on spawn/despawn to store/restore backup.
            CustomProperty = other.CustomProperty;

            // Because this method is partial and cannot be implemented for each property separately, you have to copy all properties here.
            // Or use this method as a wrapper and split execution into multiple calls.
        }
    }
}

⚠️ KCCSettings に追加されるすべてのプロパティは partial void CopyUserDataFromOther(KCCSettings other) にも追加してください。

KCCInteraction と KCCInteractions の拡張

以下の実装でKCCInteractions.Game.csファイルを作成します。

C#

namespace Fusion.KCC
{
    // Returns HUD information from interactions for displaying on screen.
    public interface IHUDProvider
    {
        string Description { get; }
    }

    // KCCInteraction is a container which contains information related to single interaction.
    // KCCInteraction is abstract class, extending it also affects derived classes (all other interaction container types).
    public abstract partial class KCCInteraction<TInteraction>
    {
        // This instance is pooled, only stateless getters!

        public IHUDProvider HUDProvider => Provider as IHUDProvider;
    }

    // KCCInteractions is a collection which maintains all interactions.
    // KCCInteractions is abstract class, extending it also extends derived classes (all other interaction collection types).
    public abstract partial class KCCInteractions<TInteraction>
    {
        // You can iterate over All property and apply custom filtering.

        public T GetHUDProvider<T>() where T : IHUDProvider
        {
            for (int i = 0, count = All.Count; i < count; ++i)
            {
                if (All[i].HUDProvider is T hudProvider)
                    return hudProvider;
            }

            return default;
        }
    }
}

KCCCollision と KCCCollisions の拡張

以下の実装でKCCCollisions.Game.csを作成します。

C#

namespace Fusion.KCC
{
    using UnityEngine;

    // Returns climb information from networked collisions.
    public interface IClimbProvider
    {
        Transform Target { get; }
    }

    // KCCCollision is a container which contains information related to a single networked collision.
    public partial class KCCCollision
    {
        // This instance is pooled, only stateless getters!

        public IClimbProvider ClimbProvider => Processor as IClimbProvider;
    }

    // KCCCollisions is a collection which maintains all networked collisions.
    public partial class KCCCollisions
    {
        // You can iterate over All property and apply custom filtering.

        // Example: returns first climb provider of a specific type.
        public T GetClimbProvider<T>() where T : IClimbProvider
        {
            for (int i = 0, count = All.Count; i < count; ++i)
            {
                if (All[i].ClimbProvider is T climbProvider)
                    return climbProvider;
            }

            return default;
        }
    }
}

KCCModifier と KCCModifiers の拡張

外部で定義された既存の型を使用する例:

C#

namespace Game
{
    // Base interface for all abilities.
    public interface IAbility
    {
        // ...
    }
}

以下の実装でKCCModifiers.Game.csファイルを作成します。

C#

namespace Fusion.KCC
{
    using System.Collections.Generic;
    using Game;

    // KCCModifier is a container which contains information related to single modifier.
    public partial class KCCModifier
    {
        // This instance is pooled, only stateless getters!

        public IAbility Ability => Processor as IAbility;
    }

    // KCCModifiers is a collection which maintains all modifiers.
    public partial class KCCModifiers
    {
        // You can iterate over All property and apply custom filter.

        // Example: returns list of abilities of a specific type.
        public bool GetAbilities<T>(List<T> abilities) where T : IAbility
        {
            abilities.Clear();

            for (int i = 0, count = All.Count; i < count; ++i)
            {
                if (All[i].Ability is T ability)
                {
                    abilities.Add(ability);
                }
            }

            return abilities.Count > 0;
        }
    }
}
Back to top