Daz3D Clothes

Daz3DでBlenderで作成したClothesを取り込む手順です。

BlenderでWavefront Objファイル形式でエクスポートします。OBJをエクスポートの 前方: -Yが前方、上: Zが上をそれぞれ指定します。

Genesis 8 Starter Essentialsをタプルクリックして Genesis 8 femaleまたはGenesis 8 maleを選択します。

File – Importを選択してWavefront Object – Objファイルを読み込みます。DAZ COLLADA – daeファイル、Autodesk FBX – fbxファイルは読み込みに失敗する場合があります。OBJ Import OptionsのFromを選択して 1 unit = 1m の項目を選択します。

Edit – Figure – Transfer Utilityを選択します。Source: のScene itemにGenesis 8 famale (male)、Target: の Scene itemにインポートした項目を指定します。Acceptをクリックします。

Unity Input System

Unity InputSystemでキーの割り当てを変更する方法です。今回はPlayer InputのCreate Actionsで生成されるInput ActionsのAction Maps -> Player -> Actions -> Fire に Space [Keyboard]を追加してそれを実行後にスクリプトから変更します。

パッケージマネージャーからInput Systemをインストールします。

オブジェクト(GameObject)に Player Input コンポーネントを追加します。Create ActionsボタンをクリックしてInput Actionsを作成します。inputactionsファイルを選択して Edit asset ボタンをクリックします。

Action Maps -> Player -> Actions -> Fire を選択します。All Control SchemesをKeyboardMouseに変更します。Fireの+ボタンをクリックしてAdd Bindingを選択します。PropertiesのBinding – Path をクリックします。Keyboard -> By Location of Key (Using US Layout)のSpaceを選択します。Use in Control Scheme の Keyboard&Mouseをチェックします。

スクリプトを追加、変更します。以下のように変更します。クラスの宣言は省略しています。

    
    public PlayerInput playerInput;
    private InputAction fireInputAction;
    private InputBinding keyboardInputBinding;

 // Start is called before the first frame update
    void Start()
    {
        playerInput = GetComponent<PlayerInput>();

        fireInputAction= playerInput.actions["Fire"];
        // keyboardInputBinding= fireInputAction.bindings[5];
        fireInputAction.ApplyBindingOverride(new InputBinding() { path = "<Keyboard>/space", overridePath = "<Keyboard>/q" });
        // fireInputAction.ApplyBindingOverride(5, "<Keyboard>/q");
        // keyboardInputBinding = fireInputAction.bindings[5];

    }

    // Update is called once per frame
    void Update()
    {

    }

    public void OnMove(InputValue value)
    {
        var wasd = value.Get<Vector2>();
    }

    public void OnLook(InputValue value)
    {

    }

    public void OnFire()
    {
        // Spaceキーの代わりにQキーを押すと以下のコートが実行されます
    }

keyboardInputBinding.path が “<Keyboard>/space” から”<Keyboard>/q” に変更されます。qは大文字、小文字どちらでも構わないようです。keyboardInputBinding.groupsはKeyboard&Mouseとなります。

ApplyBindingOverrideはInputBindingクラスを作成する方法とindexを指定する方法とあります。

ActionsのFireという名前がそのままOnFireというメソッドの名前になります。

運用としてはUIにCanvasを作成してインタラクションコンポーネントを利用して変更することになります。ほかのゲームでは左右に矢印ボタンを配置してクリックして選択したり、キー入力を受け付けたりして変更しています。

Unityの新しい入力システムInputSystemを使ってみる

https://gametukurikata.com/basic/inputsystem

こちらのサイトが参考になります。

Unity Scriptable Render Pipeline

スクリプタブルレンダーパイプラインの概要

https://blogs.unity3d.com/jp/2018/01/31/srp-overview/

こちらのブログについて Unity 2019.1 以降で CullingResults, DrawRendererSettings の個所などでエラーが発生して動作しなくなりました。

これはこのクラスがExperimentalに指定されて、Unity 2019.1 以降のバージョンでは利用できなくなりました。次のように書き換えると動作しました。

BasicAssetPipe.cs

using UnityEngine;
using UnityEngine.Rendering;
// using UnityEngine.Experimental.Rendering;


[ExecuteInEditMode]
public class BasicAssetPipe : RenderPipelineAsset
{
    public Color clearColor = Color.green;

#if UNITY_EDITOR
    [UnityEditor.MenuItem("SRP-Demo/01 - Create Basic Asset Pipeline")]
    static void CreateBasicAssetPipeline()
    {
        var instance = ScriptableObject.CreateInstance<BasicAssetPipe>();
        UnityEditor.AssetDatabase.CreateAsset(instance, "Assets/SRP-Demo/1-BasicAssetPipe/BasicAssetPipe.asset");
    }

#endif

    protected override RenderPipeline CreatePipeline()
    {
        return new BasicPipeInstance(clearColor);
    }
}

public class BasicPipeInstance : RenderPipeline
{
    private Color m_ClearColor = Color.black;

    public BasicPipeInstance(Color clearColor)
    {
        m_ClearColor = clearColor;
    }

    protected override void Render(ScriptableRenderContext context, Camera[] cameras)
    {
        // does not so much yet :()
        // base.Render(context, cameras);

        // clear buffers to the configured color
        var cmd = new CommandBuffer();
        cmd.ClearRenderTarget(true, true, m_ClearColor);
        context.ExecuteCommandBuffer(cmd);
        cmd.Release();
        context.Submit();
    }
}

OpaqueAssetPipe.cs

using System;
using UnityEngine;
using UnityEngine.Rendering;
// using UnityEngine.Experimental.Rendering;

[ExecuteInEditMode]
public class OpaqueAssetPipe : RenderPipelineAsset
{
#if UNITY_EDITOR
    [UnityEditor.MenuItem("SRP-Demo/02 - Create Opaque Asset Pipeline")]
    static void CreateBasicAssetPipeline()
    {
        var instance = ScriptableObject.CreateInstance<OpaqueAssetPipe>();
        UnityEditor.AssetDatabase.CreateAsset(instance, "Assets/SRP-Demo/2-OpaqueAssetPipe/OpaqueAssetPipe.asset");
    }
#endif

    protected override RenderPipeline CreatePipeline()
    {
        return new OpaqueAssetPipeInstance();
    }
}

public class OpaqueAssetPipeInstance : RenderPipeline
{
    protected override void Render(ScriptableRenderContext context, Camera[] cameras)
    {
        // base.Render(context, cameras);

        foreach (var camera in cameras)
        {
            // Culling
            ScriptableCullingParameters cullingParams;

            if (!(camera.TryGetCullingParameters(out cullingParams)))
                 continue;

            var cull = context.Cull(ref cullingParams);

            // Setup camera for rendering (sets render target, view/projection matrices and other
            // per-camera built-in shader variables).
            context.SetupCameraProperties(camera);

            // clear depth buffer
            var cmd = new CommandBuffer();
            cmd.ClearRenderTarget(true, false, Color.black);
            context.ExecuteCommandBuffer(cmd);
            cmd.Release();

            // Draw opaque objects using BasicPass shader pass
            var settings = new DrawingSettings(new ShaderTagId("BasicPass"), new SortingSettings(camera));
            settings.sortingSettings = new SortingSettings() { criteria = SortingCriteria.CommonOpaque };
            var filterSettings = new FilteringSettings(renderQueueRange: RenderQueueRange.opaque);
            context.DrawRenderers(cull, ref settings ,ref filterSettings);

            // Draw skybox
            context.DrawSkybox(camera);

            context.Submit();
        }
    }
}

TransparentAssetPipe.cs

using System;
using UnityEngine;
using UnityEngine.Rendering;
// using UnityEngine.Experimental.Rendering;

[ExecuteInEditMode]
public class TransparentAssetPipe : RenderPipelineAsset
{
#if UNITY_EDITOR
    [UnityEditor.MenuItem("SRP-Demo/03 - Create Transparent Asset Pipeline")]
    static void CreateBasicAssetPipeline()
    {
        var instance = ScriptableObject.CreateInstance<TransparentAssetPipe>();
        UnityEditor.AssetDatabase.CreateAsset(instance, "Assets/SRP-Demo/3-TransparentAssetPipe/TransparentAssetPipe.asset");
    }
#endif

    protected override RenderPipeline CreatePipeline()
    {
        return new TransparentAssetPipeInstance();
    }
}

public class TransparentAssetPipeInstance : RenderPipeline
{
    protected override void Render(ScriptableRenderContext context, Camera[] cameras)
    {
        // base.Render(context, cameras);

        foreach (var camera in cameras)
        {
            // Culling
            ScriptableCullingParameters cullingParams;

            if (!(camera.TryGetCullingParameters(out cullingParams)))
                continue;

            var cull = context.Cull(ref cullingParams);

            // Setup camera for rendering (sets render target, view/projection matrices and other
            // per-camera built-in shader variables).
            context.SetupCameraProperties(camera);

            // clear depth buffer
            var cmd = new CommandBuffer();
            cmd.ClearRenderTarget(true, false, Color.black);
            context.ExecuteCommandBuffer(cmd);
            cmd.Release();

            // Draw opaque objects using BasicPass shader pass
            var settings = new DrawingSettings(new ShaderTagId("BasicPass"), new SortingSettings(camera));
            settings.sortingSettings = new SortingSettings() { criteria = SortingCriteria.CommonOpaque };
            var filterSettings = new FilteringSettings(renderQueueRange: RenderQueueRange.opaque);
            context.DrawRenderers(cull, ref settings, ref filterSettings);


            // Draw skybox
            context.DrawSkybox(camera);

            settings.sortingSettings = new SortingSettings() { criteria = SortingCriteria.CommonTransparent };
            filterSettings = new FilteringSettings(renderQueueRange: RenderQueueRange.transparent);
            context.DrawRenderers(cull, ref settings, ref filterSettings);

            context.Submit();
        }
    }
}

こちらのサイトが参考になりました。

LWRP 4-preview を2019.1で動かすように改造した話

https://connect.unity.com/p/lwrp-4-preview-wo2019-1dedong-kasuyounigai-zao-shitahua

Unity Scriptable Render Pipeline(SRP)はパッケージマネージャーからUniversal Render Pipeline(URP, 2019より前はLightweight Render Pipeline(LWRP))、HD レンダーパイプライン(HDRP)をチェックしなくても利用できます。

flutter 1.15.3 dev, error

flutter_windows_1.17.2-stable では発生しません。

flutter_windows_v1.15.3-dev でビルドをで次のエラーが発生します。

Launching lib\main.dart on Android SDK built for x86 in debug mode...
Running Gradle task 'assembleDebug'...
U
nhandled exception:
Crash when compiling package:firebase_admob/firebase_admob.dart,
at character offset null:
NoSuchM
ethodError: The method 'accept' was called on null.
Receiver: null
Tried calling: accept<DartType>(Instance of '_DemotionNonNullification')
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
#1      demoteTypeInLibrary (package:front_end/src/fasta/type_inference/type_demotion.dart:65:14)
#2      TypeInferrerImpl.inferDeclarationType (package:front_end/src/fasta/type_inference/type_inferrer.dart:1889:14)
#3      InferenceVisitor.visitVariableDeclaration (package:front_end/src/fasta/kernel/inference_visitor.dart:5337:31)
#4      VariableDeclaration.accept (package:kernel/ast.dart:5495:43)
#5      TypeInferrerImpl.inferStatement (package:front_end/src/fasta/type_inference/type_inferrer.dart:3245:24)
#6      InferenceVisitor._visitStatements (package:front_end/src/fasta/kernel/inference_visitor.dart:299:20)
#7      InferenceVisitor.visitBlock (package:front_end/src/fasta/kernel/inference_visitor.dart:320:30)
#8      Block.accept (package:kernel/ast.dart:4722:43)
#9      TypeInferrerIm
pl.inferStatement (package:front_end/src/fasta/type_inference/type_inferrer.dart:3245:24)
#
10     TypeInferrerImpl.inferLocalFunction (package:front_end/src/fa
sta/type_inference/type_inferrer.dart:2595:43)
#11     Inf
erenceVisitor.visitFunctionNode (package:front_end/
src/fasta/kernel/inference_visitor.dart:1041:21)
#
12     InferenceVisitor.visitFunctionExpression (package:front_end/s
rc/fasta/
kernel/inference_visitor.dart:1065:9)
#13     FunctionExpr
ession.accept1 (package:kernel/ast.dart:4523:9
)
#14     TypeInferrerImpl.inferExpression (package:fr
ont_end/src/fasta/type_inference/type_inferrer.d
art:1988:27)
#15     InferenceVisitor.inferMapEntry (packa
ge:front_end/src/fasta/kernel/inference_visitor
.dart:1998:56)
#16     InferenceVisitor.visitMapLiteral (
package:front_end/src/fasta/kernel/inference_vis
itor.dart:2161:17)
#17     MapLiteral.accept1 (package:
kernel/ast.dart:4431:9)
#18     TypeI
nferrerImpl.inferExpression (package:front_end/src/fasta
/type_inference/type
_inferrer.dart:1988:27)
#19     _ImplicitFieldTypeRoot.
computeType (package:fr
ont_end/src/fasta/kernel/implicit_field_type.dart:138:55)
#20  
   SourceFieldBuilder.inferType (package:front_e
nd/src/fasta/builder/field_builder.dart:334:47)
#21 
    SourceLoader.performTopLevelInference (package:fro
nt_end/src/fasta/source/source_loader.dart:1109:35)
#2
2     KernelTarget.buildOutlines.<anonymous closur
e> (package:front_end/src/fasta/kernel/kernel_target
.dart:312:14)
<asynchronous s
uspension>
#23     withCrashReporting (package:front_end/sr
c/fasta/crash.dart:122:24)
#24     KernelTarget.buildOut
lines (package:front_end/src/fasta/kernel/kernel_tar
get.dart:288:12)
#25     generateKernelInternal.<anonymous
 closure> (package:front_end/src/kernel_generat
or_impl.dart:108:28)
<asynchronous suspension>
#26     withCrashReporting (package:fro
nt_end/src/fasta/crash.dart:122:24)
#27     generateKernelInte
rnal (package:front_end/src/kernel_generator_impl.dart:70:10)
#28     ke
rnelForProgramInternal.<anonymous closure> (package:front_end/src/
api_prototype/kernel_generator.dart:61:35)
#29     Compiler
Context.runWithOptions.<anonymous closure> (package:front_end/sr
c/fasta/compiler_context.dart:135:20)
<asynchronous suspension>

#30     CompilerC
ontext.runInContext.<anonymous closure>.<anonymous closure> (pack
age:front_end/src/fasta/compiler_context.dart:123:
46)
#31     new Future.sync (dart:async/future.dar
t:224:31)
#32     CompilerContext.runInContext.<anonymous c
losure> (package:front_end/src/fasta/compiler_context.dart:123:1
9)
#33     _rootRun (dart:async/zone.dart:1126:13)
#34     _Cus
tomZone.run (dart:async/zone.dart:1023:19)
#35     _
runZoned (dart:async/zone.dart:1518:10)
#36     runZoned 
(dart:async/zone.dart:1465:12)
#37     CompilerContext.
runInContext (package:front_end/src/fasta/compiler_con
text.dart:122:12)
#38     CompilerContext.runWithOpt
ions (package:front_end/src/fasta/compiler_context.da
rt:133:10)
#39     kernelForProgramInternal (package:f
ront_end/src/api_prototype/kernel_generator.dart
:60:32)
#40     kernelForProgram (package:front_end/
src/api_prototype/kernel_generator.dart:52:17)
#
41     compileToKernel (package:vm/kernel_front_end.da
rt:319:41)
#42     FrontendCompiler.compile.<anonymou
s closure> (package:frontend_server/frontend_ser
ver.dart:452:54)
#43     new Future.<anonymous closure>
 (dart:async/future.dart:176:37)
#44     _rootRun
 (dart:async/zone.dart:1122:38)
#45     _CustomZon
e.run (dart:async/zone.dart:1023:19)
#46     _CustomZone.ru
nGuarded (dart:asyn
c/zone.dart:925:7)
#47     _CustomZone.bindCallbackGuarded.<a
nonymous closure> (dart:async/zone.dart:965:23)
#4
8     _rootRun (dart:async/zone.dart:1126:13)
#49   
  _CustomZone.run (dart:async/zone.dart:1023:19)
#
50     _CustomZone.bindCallback.<anonymous closure> (da
rt:async/zone.dart:949:23)
#51     Timer._createTimer.
<anonymous closure> (dart:async-patch/timer_patch.dart:23:15)
#52     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:398:19)
#53     _Ti
mer._handleMessage (dart:isolate-patch/t
imer_impl.dart:429:5)
#54     _RawReceivePortImpl._handleMess
age (dart:isola
te-patch/isolate_patch.dart:168:12)


#0      Object.noSuchMet
hod (dart:core-patch/object_patch.dart:53:5)
#1      d
emoteTypeInLibrary (package:front_end/src/fasta/type_infe
rence/type_demotion.dart:65:14)
#2      TypeInferrerImpl.inferDec
larationType (package:fr
ont_end/src/fasta/type_inference/type_inferrer.dart:1889:
14)
#3      InferenceVis
itor.visitVariableDeclaration (package:front_end/src/fast
a/kernel/inference_visitor.dart:5337:31)
#4      Var
iableDeclaration.accept (package:kernel/ast.dart:549
5:43)
#5      TypeInferrerImpl.inferStatement (pack
age:front_end/src/fasta/type_inference/type_infe
rrer.dart:3245:24)
#6      InferenceVisitor._visitStatem
ents (package:front_end/src/fasta/kernel/inferen
ce_visitor.dart:299:20)
#7      InferenceVisitor.vi
sitBlock (package:front_end/src/fasta/kernel/inf
erence_visitor.dart:320:30)
#8      Block.accept (package:kernel/ast.dart:
4722:43)
#9      TypeInferrerIm
pl.inferStatement (package:front_end/src/fasta/type_inference/
type_inferrer.dart:3245:24)
#
10     TypeInferrerImpl.inferLocalFunction (package:f
ront_end/src/fasta/type_inference/type_inferrer.dart:
2595:43)
#11     InferenceVisitor.visi
tFunctionNode (package:fro
nt_end/src/fasta/kernel/inference_visitor.dart:1041:2
1)
#12     InferenceVisitor.visitFunctionExpr
ession (package:front_end/src/fas
ta/kernel/inference_visitor.dart:1065:9)
#13     FunctionExpression.accept1 (package:kernel/ast.dart:4523:9)
#
14     TypeInferrerImpl.inferExpression (package:front_end/src/fasta/type_inference/t
ype_inferrer.dart:1988:27)
#15     InferenceVisitor.inferMapEntry (package:front_en
d/src/fasta/kernel/inference_visitor.dart:1998:56)
#16     In
ferenceVisitor.visitMapLiteral (package:front_end/src/fas
ta/kernel/infe
rence_visitor.dart:2161:17)
#17     MapLiteral.accept1 (p
ackage:kernel/ast.dart:4431:9)
#18     TypeInferrerImp
l.inferExpression (package:front_end/src/fasta/type_infer
ence/type_inferrer.dart:1988:27)
#19     _ImplicitFieldT
ypeRoot.computeType (package:front_end/src/fasta/kern
el/implicit_field_type.dart:138:55)
#20     SourceFiel
dBuilder.inferType (package:front_end/src/fasta/buil
der/field_builder.dart:334:47)
#21     SourceLoader.per
formTopLevelInference (package:front_end/src/fasta/sou
rce/source_loader.dart:1109:35)
#22     KernelTarget.buildOu
tlines.<anonymous closure> (package:front_end/src/fa
sta/kernel/kernel_target.dart:312:14)
<asynchronous suspen
sion>
#23     withCrashReporting (package:front_end
/src/fasta/crash.dart:122:24)
#24     KernelTarget.build
Outlines (package:front_end/src/fasta/kernel/kernel_tar
get.dart:288:12)
#25     generateKernelInternal.<anonym
ous closure> (package:front_end/src/kernel_generator_imp
l.dart:108:28)
<asynchronous suspension>
#26     withCr
ashReporting (package:front_end/src/fasta/crash.dart
:122:24)
#27     generateKernelInternal (package:front_en
d/src/kernel_generator_impl.dart:70:10)
#28     ker
nelForProgramInternal.<anonymous closure> (package:fron
t_end/src/api_prototype/kernel_generator.dart:61:35)
#29
     CompilerContext.runWithOptions.<anonymous closure> (packa
ge:front_end/src/fasta/compiler_context.dart:135:20)
<asynchronous s
uspension>
#30     CompilerC
ontext.runInContext.<anonymous closure>.<anonymous closure> (packa
ge:front_end/src/fasta/compiler_context.dart:123:46)
#31     new Futur
e.sync (dart:async
/future.dart:224:31)
#32     CompilerContext.runInContext.<anonymous closur
e> (package:fr
ont_end/src/fasta/compiler_context.dart:123:19)
#33     _rootRun (dart:async
/zone.dart:1126:13)
#34     _
CustomZone.run (dart:async/zone.dart:1023:19)
#35     _runZoned (dart
:async/zone.dart:1518:10)
#36     runZoned (dart:async/zone.dart
:1465:12)
#37     CompilerContext.runInContext (package:front_end/
src/fasta/compiler_context.dart:122:12)
#38     CompilerContext.runWit
hOptions (package:front_end/src/fasta/compiler_context.dart:133:
10)
#39     kernelForProgramInternal (package:fr
ont_end/src/api_prototype/kernel_generator.dart:60:32)
#40     kernelForPr
ogram (package:front_end/src/api_prototype/kernel_generator.dart:52:17)
#41     compileToK
ernel (package:vm/kernel_front_end.dart:319:41)
#42     FrontendCompiler.compile.<anony
mous closure> (package:frontend_server/frontend_server.dart:452:54)
#43     new Futur
e.<anonymous closure> (dart:async/future.dart:176:37)
#44     _rootRun (dart:async/zo
ne.dart:1122:38)
#45     _CustomZone.run (dart:async/zone.dart:1023:19)
#46     _CustomZo
ne.runGuarded (dart:async/zone.dart:925:7)
#47     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:965:23)
#48     _root
Run (dart:async/zone.dart:1126:13)
#49     _CustomZone.run (dart:async/zone.dart:1023:19)
#50     _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:949:23)
#51     Timer._createTimer.<a
nonymous closure> (dart:async-patch/timer_patch.dart:23:15)
#52     _Timer._runTimers (dart:isolate-pa
tch/timer_impl.dart:398:19)
#53     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:
429:5)
#54     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)

Target kernel_snapshot failed: Exception: Errors during snapshot creation: null
build failed.

FAILURE: Build failed with an exception.

* Where:
Script 'D:\src\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 817

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'D:\src\flutter\bin\flutter.bat'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 9s
Finished with error: Gradle task assembleDebug failed with exit code 1

flutter_windows_v1.12.13+hotfix.8-stable では発生しません。

dart, add XmlElement

dart, flutter で XmlElementを追加する方法です。

import 'package:flutter/material.dart';
import 'package:xml/xml.dart' as xml;
import 'dart:async';
import 'dart:io';
import 'dart:core';

var bookshelfXml = '''<?xml version="1.0"?>
    <bookshelf>
      <book>
        <title lang="english">Growing a Language</title>
        <price>29.99</price>
      </book>
      <book>
        <title lang="english">Learning XML</title>
        <price>39.95</price>
      </book>
    </bookshelf>''';
var document = xml.parse(bookshelfXml);

var element_book = xml.XmlElement(xml.XmlName('book'));
var element_title = xml.xmlElement(xml.XmlName('title'))..attributes.add(xml.XmlAttribute(xml.XmlName('lang'), 'english'))..children.add(xml.XmlText('My book'));
var element_price = xml.XmlElement(xml.XmlName('price'))..children.add(xml.XmlText('19.95'));
element_book..children.add(element_title);
element_book..children.add(element_price);
document.rootElement.children.add(element_book);

この方法でbook要素に title が My book, price が 19.95 の book が追加されます。

<?xml version="1.0"?>
    <bookshelf>
      <book>
        <title lang="english">Growing a Language</title>
        <price>29.99</price>
      </book>
      <book>
        <title lang="english">Learning XML</title>
        <price>39.95</price>
      </book>
      <book>
        <title lang="english">My book</title>
        <price>19.95</price>
      </book>
    </bookshelf>

children.add に別のXmlDocumentのXmlNode, XmlElement などを配置すると例外が発生します。xml.XmlElement(xml.XmlName(“book”)) 等で生成または、xml.XmlElement.copy() を使用する必要があります。

Call Kotlin method from Flutter(FlutterからKotlinのコード内のメソッドを呼び出す)

MainActivity.kt (Kotlin)

package (your.app)

import android.content.res.Configuration
import android.os.Bundle
import android.os.PersistableBundle
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant

import io.flutter.embedding.android.FlutterFragment
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel

import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result

import io.flutter.plugin.platform.PlatformViewsController


class MainActivity: FlutterActivity() {
    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine)

        MethodChannel(flutterEngine.dartExecutor, CHANNEL).setMethodCallHandler { methodCall, result ->
            val args = methodCall.arguments
            when (methodCall.method) {
                "method1" -> {
                    val number = 10
                    result.success(number)
                }
            }
        }
    }

    companion object {
        private val CHANNEL = "com.your.app.flutter/method1"
    }

}

Dart

static const platform = const MethodChannel('com.your.app.flutter/method1');

Future<void> _Method1() async{
    int _number;
    try {
      final int result = await platform.invokeMethod('method1');
      _number = result;

      setState(() {
        number = _number;
      });

    } on PlatformException catch (e)
    {
      setState(() {
        number = -1;
      });
    }
}

int number;

Widget ShowNumber()
{
  Text(number.toString());
}

_Method1() を呼び出すと Kotlin側のmethod1 -> {} が実行され、結果が Flutter側の result に戻ります。

String testString = “abc”;
platform.invokeMethod(‘method1’, testString); でtestStringを引数にできます。
Kotlin側の methodCall.arguments で引数を取得できます。

io.flutter.app.FlutterActivity の FlutterActivity には FlutterEngine が存在しないのでGeneratedPluginRegistrant.registerWith(this) ではregistできません。またMethodCall の flutterView の代わり(java では getFlutterView() )に flutterEngine.dartExecutor を使用します。

2019-12-05の時点で app/build.gradle 内で Cannot resolve symbol ‘GradleException’ が、MainActivity.kt で Unresolved reference: NonNull がそれぞれ赤字で表示されます。2019-12-07のAndroid Studio アップデートとFlutter Pluginのアップデートで表示されなくなったようです。

こちらのサイトの情報が参考になりました。

https://github.com/flutter/flutter/issues/41102
https://qiita.com/tasogarei/items/bf3761139d46c43d5896
https://qiita.com/unsoluble_sugar/items/ae42b5faf52a491f6470
https://flutter.keicode.com/basics/method-channel-java.php